12

我使用 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"
            ),
        ),
    ),
));

您可以看到字段“名称”有过滤器和验证器。有用。如果我使用字段集,我会遇到问题:

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

3 回答 3

18

使用字段集时,您需要在输入过滤器中指定“类型”键。

$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());
}
于 2013-04-30T15:04:08.643 回答
2

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

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

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

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

2getInputFilter()会让你头疼。

于 2016-08-11T18:44:34.253 回答
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"
        ),
    ),
),
));
于 2013-04-30T08:09:19.127 回答