0

我有一个使用 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;
    }
}
4

1 回答 1

0

我认为您需要将输入过滤器添加到您要动态添加的字段集的 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',
            )
        ));
    }
}
于 2013-12-20T20:44:39.533 回答