验证后 Zend Form 2 多选字段为空
Zend Form 2 multiselect field is empty after validation
我不确定发生了什么。我正在使用带有多选字段的 Zend Form 2。当我提交代码时,值存在于 post 中。当我 运行 通过 zend form 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>');
}
}
}
转储 1:
Array
(
[author] => Array
(
[0] => check1
[1] => check2
)
}
转储 2:
Array
(
[author[]] =>
)
我意识到我可以很容易地绕过这里的验证,因为我没有在该字段上使用任何验证器。不过我更关心根本原因。
为什么经过验证的作者数据为空?
当您在属性中指定multiple
时,Zend\Form
和Zend\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' => [],
]);
我不确定发生了什么。我正在使用带有多选字段的 Zend Form 2。当我提交代码时,值存在于 post 中。当我 运行 通过 zend form 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>');
}
}
}
转储 1:
Array
(
[author] => Array
(
[0] => check1
[1] => check2
)
}
转储 2:
Array
(
[author[]] =>
)
我意识到我可以很容易地绕过这里的验证,因为我没有在该字段上使用任何验证器。不过我更关心根本原因。
为什么经过验证的作者数据为空?
当您在属性中指定multiple
时,Zend\Form
和Zend\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' => [],
]);