Zend Framework 2 inputfilter incorrect behaviour

510 Views Asked by At

there is a more than strange behavior of filterInput, the getting filter function itself is:

public function getInputFilter($id = null){
        if (!$this->inputFilter){
            $inputFilter = new InputFilter();
            $factory     = new InputFactory();

            $id = intval($id);

            $inputFilter->add($factory->createInput(array(
                'name'     => 'name',
                'required' => true,

                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),

                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                    array(
                        'name' => 'Db\NoRecordExists',
                        'options' => array(
                            'field'   => 'name',
                            'table'   => 'table',
                            'adapter' => $this->dbAdapter,
                            'message' => 'record exists',
                            'exclude' => array(
                                'field' => 'id',
                                'value' => $id
                            )
                        ),
                    )
                ),
            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

setting the filter like:

$form->setInputFilter($model->getInputFilter($id));

Now, when we fire $form->isValid(), the validation error will rise about duplication in the database, if I will remove Db\NoRecordExists validator, the database will contain 2 records! more interesting, if I will set 'required' => false, there will be no double inserting, the same with adding second validation field. Working settings are:

public function getInputFilter($id = null){
        if (!$this->inputFilter){
            $inputFilter = new InputFilter();
            $factory     = new InputFactory();

            $id = intval($id);

            $inputFilter->add($factory->createInput(array(
                'name'     => 'name',
                'required' => true,

                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),

                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                    array(
                        'name' => 'Db\NoRecordExists',
                        'options' => array(
                            'field'   => 'name',
                            'table'   => 'table',
                            'adapter' => $this->dbAdapter,
                            'message' => 'record exists',
                            'exclude' => array(
                                'field' => 'id',
                                'value' => $id
                            )
                        ),
                    )
                ),
            )));
            //test field
            $inputFilter->add($factory->createInput(array(
                'name'     => 'id',
                'required' => false,

                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array('name' => 'Int')
                ),

            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

So it fails to work correctly with filter config for only one field.. Does anybody knows the reason?

0

There are 0 best solutions below