【问题标题】:ZF2 How to validate fields inside fieldset?ZF2 如何验证字段集中的字段?
【发布时间】:2014-05-03 04:12:14
【问题描述】:

我有一个这样的表单字段:

    $this -> add(array(
        'type' => 'field-set',
        'name' => 'meta_properties',
        'options' => array(
            'label' => "Meta Properties",
        ),
        'elements' => array(

        )
    ));

    $meta_fieldSet = $this -> get('meta_properties');

    $meta_fieldSet->add(array(
        'name' => 'meta_title',
        'attributes' => array(
            'type'  => 'text',
        ),
        'options' => array(
            'label' => 'Title',
        ),
    ));   

    $meta_fieldSet->add(array(
        'name' => 'meta_description',
        'attributes' => array(
            'type'  => 'text',
        ),
        'options' => array(
            'label' => 'Description',
        ),
    ));

    $meta_fieldSet->add(array(
        'name' => 'meta_keywords',
        'attributes' => array(
            'type'  => 'text',
        ),
        'options' => array(
            'label' => 'Keywords',
        )
    ));

在我的输入过滤器类中,我有

     $inputFilter -> add($factory -> createInput(array(
            'name' => 'meta_title',
            'required' => false,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 5,
                        'max' => 150,
                    ),
                ),
            ),
        )));

        $inputFilter -> add($factory -> createInput(array(
            'name' => 'meta_description',
            'required' => false,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 5,
                        'max' => 150,
                    ),
                ),
            ),
        )));

        $inputFilter -> add($factory -> createInput(array(
            'name' => 'meta_keywords',
            'required' => false,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 5,
                        'max' => 150,
                    ),
                ),
            ),
        )));

它没有验证。如何验证 InputFilter?

【问题讨论】:

    标签: validation zend-framework2 fieldset


    【解决方案1】:

    创建 Fieldsets 时,您需要扩展 \Zend\Form\Fieldset。尝试像这样创建一个字段集:

    <?php
    /**
     * Fieldset Example
     */
    namespace Module\Form;
    
    use Zend\Form\Fieldset;
    use Zend\InputFilter\InputFilterProviderInterface;
    use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
    
    class NewFieldset extends Fieldset implements InputFilterProviderInterface
    {
        public function __construct()
        {
            // we want to ignore the name passed and give it our own
            parent::__construct('appointment');
    
            // Use the model you are associating with your fieldset here
            $this->setHydrator(new ClassMethodsHydrator(false))
                 ->setObject(new MyModel)
                 ->setLabel('My Model');
    
            $this->add(array(
                'name' => 'fieldset_item_1',
                'type' => 'Select',
                'options' => array(
                    'label' => 'FieldSet Item 1',
                )
            ));
    
            $this->add(array(
                'name' => 'fieldset_item_2',
                'type' => 'Select',
                'options' => array(
                    'label' => 'FieldSet Item 2',
                )
            ));
        }
    
        /**
         * Sets up the input filter specification and returns it
         */
        public function getInputFilterSpecification()
        {
            return array(
                'fieldset_item_1' => array(
                    'required' => false,
                    'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name'    => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min'      => 1,
                                'max'      => 100,
                            ),
                        ),
                    ),
                ),
                'fieldset_item_2' => array(
                    'required' => false,
                    'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name'    => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min'      => 1,
                                'max'      => 100,
                            ),
                        ),
                    ),
                ),
            );
        }
    }
    

    然后你可以像这样在你的表单中使用它:

    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'fieldset',
        'options' => array(
            'label' => '',
            'count' => 1,
            'should_create_template' => true,
            'allow_add' => true,
            'use_as_base_fieldset' => false,
            'create_new_objects' => true,
            'target_element' => array('type' => 'Module\Form\NewFieldset')
        ),
    ));
    

    现在,当您在控制器中执行 $form-&gt;isValid() 时,它将验证字段集。此外,您现在可以编写一些花哨的前端代码来从表单中动态添加和删除字段集,它们都会得到验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多