Validating Zend\Form\Element\Collection

456 views Asked by At

I have a form which has 'rows' added dynamically using Zend\Form\Element\Collection. This works fine, but I am struggling to add the validation for these rows.

So far my code looks something like the following. I presume I need to pass something to InputFilter\InputFilter::add() but I can't figure out what:

<?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;
    }
}
1

There are 1 answers

0
Purple Hexagon On

I think you need to add the input filter to the getInputFilterSpecification method of the fieldset you are adding dynamically

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'
                         )
                    )
                )
            );
        }    

then in your form you need to set the validation group

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',
            )
        ));
    }
}