Using ZF2, I wrote a custom form Element and include it on a bunch of forms. The problem is that if I specify that I don't want the form element to be required, I lose the default validators on 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 appears that by including "emails" in getInputSpecification(), the default validator on EmailList is completely wiped out and never used.
How do I go about setting the required flag to false and maintain the element's default validator?
Note, this custom field is used in a bunch of forms and most of the time is required, which is why its default specification includes setting the required flag to true.
Thanks
CollectionInputFilter. I ran into issues last week and created a Pull Request - Bram Gerritsen