The zend2 configuration field is not required without losing the default input filter element

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

+7
php zend-framework zend-framework2
source share
1 answer

So, you want the email field to be optional, but if it is filled in, to use your validator?

Assuming I understood this problem correctly, your code works fine for me (on ZF 2.2.5) with one very small change:

 class EmailList extends \Zend\Form\Element\Email { protected $attributes = array( 'type' => 'text', // <- I changed type from 'email' to 'text' 'multiple' => true, ); [...] 

HTML type="email" is one of the field types added in HTML5, and (at least in Webkit browsers) this causes some client-side validation in the browser itself. One thing the browser will do with this type of field is remove any spaces. Your form then cannot be validated because you want (comma) as a separator. Changing the type to text , the browser will consider it as a simple text field, and ZF will process the entire server check.

If this is a problem, you will probably find that setting the value separator to , (or not setting it at all) will also fix the problem.

If this does not fix, make sure that the application goes into the condition if ($validator instanceof ExplodeValidator) { . You can also try to look at the validators attached to the form element with:

 var_dump($form->getInputFilter()->get('emails')->getValidatorChain()); 
+2
source share

All Articles