Ildar Ildar - 3 months ago 51
PHP Question

How to set filters and validators in ZF2 fieldsets using Zend\Form\Factory?

I use Zend\Form\Factory to create forms in zend framework2

$factory = new Zend\Form\Factory();
$factory->createForm(array(
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
));


You can see that there are filters and validators for field "name". It works.
I have the problem if I use fieldsets:

$factory->createForm(array(
'fieldsets' => array(
array(
'spec' => array(
'name' => 'common',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
));


In this example input filter doesn`t work. I don't know how to set filters and validators to field "name" in fieldset "common"

This example does not work too:

$factory->createForm(array(
'fieldsets' => array(
array(
'spec' => array(
'name' => 'common',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
),
),
),
));

Answer

You need to specify 'type' key in input filter when you used fieldset.

$factory = new \Zend\Form\Factory();        
$form    = $factory->createForm(array(
    'hydrator'  => 'Zend\Stdlib\Hydrator\ArraySerializable',
    'elements' => array(
        array(
            'spec' => array(
            'name' => 'email1',
            ),
        ),
    ),  
    'fieldsets' => array(
        array(
            'spec' => array(
                'name' => 'common',
                'elements' => array(
                    array(
                        'spec' => array(
                        'name' => 'email2',
                        ),
                    ),
                ),
            ),
        ),
    ),
    'input_filter' => array(                
        'email1' => array(
            'validators' => array(
            // validators for field "name"                        
                new \Zend\Validator\EmailAddress(),
            ),
            'filters' => array(
            // filters for field "name"
                array('name' => 'Zend\Filter\StringTrim'),
            ),
         ),
        'common' => array(
            'email2' => array(
                'validators' => array(
                // validators for field "name"                        
                new \Zend\Validator\EmailAddress(),
                ),
                'filters' => array(
                // filters for field "name"
                array('name' => 'Zend\Filter\StringTrim'),
                ),
            ),
            'type' => 'Zend\InputFilter\InputFilter',
         )          
    ),

));

$form->setData(array('email1'=>'test@gmail.com','common'=>array('email2'=>'invalid-email')));
if(!$form->isValid()){
    print_r($form->getMessages());
}