Zend Framework 2 inputfilter行为不正确

时间:2013-08-20 13:50:58

标签: zend-framework2 zend-form zend-filter

filterInput有一个奇怪的行为,获取过滤器函数本身是:

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

设置过滤器:

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

现在,当我们触发$ form-> isValid()时,验证错误将在数据库中重复出现,如果我将删除Db \ NoRecordExists验证器,则数据库将包含2条记录!更有趣的是,如果我设置'required'=> false,不会有双重插入,与添加第二个验证字段相同。工作设置是:

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

因此,只有一个字段的过滤器配置无法正常工作..有人知道原因吗?

0 个答案:

没有答案