验证后,Zend Form 2多选字段为空

时间:2017-10-19 23:28:09

标签: php zend-framework2 zend-form

我不确定发生了什么。我使用Zend Form 2和多选字段。当我提交代码时,值存在于post中。当我通过zend表单2运行值时,我没有得到验证错误,但是多选字段突然变空。

class Form extends \Zend\Form\Form
{
    // input filter to set up filters and validators
    protected $myInputFilter;

    public function __construct()
    {
        // create the zend form
        parent::__construct();

        // make it a bootstrap form
        $this->setAttribute('class', 'form-horizontal');
        $this->setAttribute('role', 'form');

        // set the default objects we'll use to build the form validator
        $this->myInputFilter = new \Zend\InputFilter\InputFilter();
    }
}

class AddPublicationForm extends Form
{
    public function __construct()
    {
        // create the zend form
        parent::__construct();

        $this->setAttribute('class', 'form-horizontal');
        $this->setAttribute('id', 'add-publication-form');

        $this->add([
            'name' => 'author[]',
            'attributes' => [
                'class' => 'form-control',
                'data-placeholder' => 'Author',
                'multiple' => 'multiple',
                'placeholder' => 'Author',
            ],
            'required' => false,
            'type'       => \Zend\Form\Element\Select::class,
            'options' => [
                'value_options' => [
                    'check1' => 'check1',
                    'check2' => 'check2',
                ],
            ],
        ]);

        $this->myInputFilter->add([
            'filters' => [],
            'name' => 'author[]',
            'required' => false,
            'validators' => [],
        ]);

        // attach validators and filters
        $this->setInputFilter($this->myInputFilter);

        // prepare the form
        $this->prepare();
    }
}

这些是我正在使用的zend表单对象。我使用Slim Framework 2作为我的后端。这是控制器对象:

public function addAction()
{
    $request = $this->app->request;
    $form = new Form\AddPublicationForm();

    if ($request->isPost()) {
        $params = $request->params();
        // DUMP 1: exit('<pre>'.print_r($params, true).'</pre>');
        $form->setData($params);

        if ($form->isValid()) {
            $data = $form->getData();
            // DUMP 2: exit('<pre>'.print_r($data, true).'</pre>');
        }
    }
}

DUMP 1:

Array
(
    [author] => Array
        (
            [0] => check1
            [1] => check2
        )
}

DUMP 2:

Array
(
    [author[]] =>
)

我意识到我可以很容易地绕过这里的验证,因为我没有在该字段上使用任何验证器。我更关注根本原因。

为什么经过验证的作者数据为空?

1 个答案:

答案 0 :(得分:0)

在属性中指定multiple时,Zend\FormZend\InputFilter会在名称后添加[]。你不应该自己做,否则,在html代码中,元素显示在名称author[][]下,setData方法不匹配。 要查看它,请将required替换为true,然后查看表单的html代码。

$this->add([
        'name' => 'author',
        'attributes' => [
            'class' => 'form-control',
            'data-placeholder' => 'Author',
            'multiple' => 'multiple',
            'placeholder' => 'Author',
        ],
        'required' => false,
        'type'       => \Zend\Form\Element\Select::class,
        'options' => [
            'value_options' => [
                'check1' => 'check1',
                'check2' => 'check2',
            ],
        ],
    ]);

    $this->myInputFilter->add([
        'filters' => [],
        'name' => 'author',
        'required' => false,
        'validators' => [],
    ]);