【问题标题】:Validating Zend\Form\Element\Collection验证 Zend\Form\Element\Collection
【发布时间】:2014-01-09 12:58:16
【问题描述】:

我有一个使用 Zend\Form\Element\Collection 动态添加“行”的表单。这工作正常,但我正在努力为这些行添加验证。

到目前为止,我的代码如下所示。我想我需要向 InputFilter\InputFilter::add() 传递一些东西,但我不知道是什么:

<?php

class EditForm extends \Zend\Form\Form
{
    public function __construct()
    {
        parent::__construct('edit');
        $this->setUpFormElements();
        $this->setupInputFilters();
    }

    protected function setUpFormElements()
    {
        $fieldset = new \Zend\Form\Fieldset;

        $nameElement = new \Zend\Form\Element\Text('name');
        $fieldset->add($nameElement);

        $descriptionElement = new \Zend\Form\Element\Text('description');
        $fieldset->add($description);

        $this->add(
            array(
                'type' => 'Zend\Form\Element\Collection',
                'name' => 'rows',
                'options' => array(
                    'label' => 'Edit Rows',
                    'should_create_template' => true,
                    'allow_add' => true,
                    'target_element' => $fieldset,
                )
            )
        );

        return $this;
    }

    public function setupInputFilters()
    {
        $filter = new \Zend\InputFilter\InputFilter();

        $filter->add(
            array(
                'name' => 'rows',
                'required' => true,
                'validators' => array(
                    // Not sure what to do here!
                )
            )
        );

        return $this;
    }
}

【问题讨论】:

    标签: zend-framework2 zend-form2


    【解决方案1】:

    我认为您需要将输入过滤器添加到您要动态添加的字段集的 getInputFilterSpecification 方法中

    class Row extends Fieldset implements InputFilterProviderInterface
    {
    
            $this->add(array(
                'name' => 'yourFieldset',
                'options' => array(
                    'label' => 'One of your fieldset elements'
                ),
                'attributes' => array(
                    'required' => 'required'
                )
            ));
    
    
            $this->add(array(
                'name' => 'fields',
                'options' => array(
                    'label' => 'Another fieldset element'
                ),
                'attributes' => array(
                    'required' => 'required'
                )
            ));
    
            public function getInputFilterSpecification()
            {
                return array(
                    'yourFieldset' => array(
                        'required' => true,
                      ),
                    'fields' => array(
                        'required' => true,
                        'validators' => array(
                             array(
                                'name' => 'Float'
                             )
                        )
                    )
                );
            }    
    

    那么你需要在你的表单中设置验证组

    class EditForm extends Form
    {
        public function __construct()
        {
            $this->add(
             array(
                 'type' => 'Zend\Form\Element\Collection',
                 'name' => 'rows',
                 'options' => array(
                     'label' => 'Edit Rows',
                     'should_create_template' => true,
                     'allow_add' => true,
                     'target_element' => $fieldset,
                 )
                 )
            );
    
            $this->setValidationGroup(array(
                'csrf',
                'row' => array(
                    'yourFieldset',
                    'fields',
                )
            ));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多