Using ZF2, I wrote a custom form element and included it in a bunch of forms. The problem is that if I specify that I do not want the form element to be required, I lose the default validators for the element.
class MyForm extends Zend\Form\Form implements Zend\InputFilter\InputFilterProviderInterface { public function __construct() { parent::__construct("my-form"); $this ->add(array( 'type' => 'Me\Custom\EmailList', 'name' => 'emails', 'options' => array( 'label' => _t('Email List'), ), )); } public function getInputFilterSpecification() { return array( 'emails' => array( 'required' => false, ), )); } }
The EmailList element is a simple text field that accepts a comma-separated list of email addresses.
class EmailList extends \Zend\Form\Element\Email { protected $attributes = array( 'type' => 'email', 'multiple' => true, ); public function getInputSpecification() { $this->getEmailValidator() ->setMessage('"%value%" is not a valid email address'); $validator = $this->getValidator(); if ($validator instanceof ExplodeValidator) { $validator->setValueDelimiter(', '); } return array( 'name' => $this->getName(), 'required' => true, 'validators' => array( $validator, ), ); } }
So, in my MyForm class, it seems that by including "emails" in getInputSpecification() , the default validator in the EmailList is completely destroyed and never used.
How do I set the flag value to false and keep the default validator for the item?
Note. This custom field is used in a bunch of forms, and most of the time is required, so its default specification includes setting the required flag to true.
thanks
php zend-framework zend-framework2
jbarreiros
source share