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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:05:23

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());
}

If you want to add dynamic validators in the Action (for example validators that are required only when some other fields have a specific value), it is quite a puzzle to apply this when using form collection.

In order to achieve this you should grab the validator chain from the specific element. For each fieldset however, you should first hook in it's own input filter. I would like to share this, because this took me 2 hours to understand ;)

Let's say you have a base form, the base form has a fieldset, and the fieldset has x-elements. The code to add a validator to one of the x-elements requires following chain:

$form->getInputFilter()
     ->get('base-form')
     ->get('fieldset-form')
     ->getInputFilter()
     ->get('element')
     ->getValidatorChain()
     ->addValidator($validator);

The 2 getInputFilter() can give you an headache.

You have your syntax incorrect, are common and spec supposed to be nested fieldsets or something? Not sure what you are doing there... Try removing the spec part

$factory = new Factory();
$form    = $factory->createForm(array(
'fieldsets' => array(
    array(
        'name' => 'details',
        /**
         * Elements for the "details" form
         */
        'elements' => array(
            array(
                'name' => 'name',
                'type'  => 'Text',
                'options' => array(
                    'label' => 'Full name',
                    ),

            ),
            array(
                'type' => 'Zend\Form\Element\Email',
                'name' => 'email',
                'options' => array(
                    'label' => 'Email address',
                    ),
            ),
        ),
    ),
    array(
        'name' => 'extra',
        'elements' => array(
            array(
                'name' => 'address',
                'type'  => 'Text',
                'options' => array(
                    'label' => 'Address',
                ),
            ),
            array(
                'name' => 'notes',
                'type' => 'Zend\Form\Element\Textarea',
                'options' => array(
                    'label' => 'Notes',
                ),
            ),
        ),
    ),
),
/**
 * Elements on the form itself, not in the fieldsset
 */
'elements' => array(
    array(
        'type' => 'Zend\Form\Element\Captcha',
        'name' => 'captcha',
        'options' => array(
            'captcha' => array(
                'class' => 'Dumb',
            ),
        ),
    ),
    array(
        'type' => 'Zend\Form\Element\Csrf',
        'name' => 'security',
    ),
    array(
        'name' => 'send',
        'type'  => 'Submit',
        'attributes' => array(
            'value' => 'Submit',
        ),
    ),
),
/*/
 * Input Filters Spec here
 */
'input_filter' => array(
    'name' => array(
        'validators' => array(
            // validators for field "name"
        ),
        'filters' => array(
            // filters for field "name"
        ),
    ),
),
));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!