如何使用Zend \ Form \ Factory在ZF2字段集中设置过滤器和验证器?

时间:2013-04-26 22:17:14

标签: php zend-framework2

我使用Zend \ Form \ Factory在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"
            ),
        ),
    ),
));

您可以看到字段“name”有过滤器和验证器。有用。 如果我使用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"
            ),
        ),
    ), 
));

在此示例中,输入过滤器不起作用。我不知道如何设置过滤器和验证器来在字段集“common”

中填写“name”字段

此示例也不起作用:

$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"
                        ),
                    ),
                ), 
            ),
        ),
    ),       
));

3 个答案:

答案 0 :(得分:18)

使用fieldset时,需要在输入过滤器中指定“type”键。

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

答案 1 :(得分:2)

如果要在Action中添加动态验证器(例如,仅当某些其他字段具有特定值时才需要验证器),在使用表单集合时应用此验证非常困难。

为了实现这一目标,您应该从特定元素中获取验证器链。但是,对于每个字段集,您应首先挂入它自己的输入过滤器。我想分享一下,因为这花了我2个小时才明白;)

假设您有一个基本表单,基本表单有一个字段集,而字段集有x元素。将验证器添加到其中一个x元素的代码需要以下链:

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

2 getInputFilter()会让你头疼。

答案 2 :(得分:1)

你的语法不正确,常见且规格应该是嵌套的字段集还是什么?不确定你在那里做什么...尝试删除规范部分

$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"
        ),
    ),
),
));