Ant*_*_Sh 5 php validation zend-framework2
我试图使用输入过滤器验证乘法选择,但每次看到错误.错误是"notInArray":"在大海捞针中找不到输入".(我使用ajax,但它并不是完美的).我将更清楚地展示我的部分代码.
在控制器中:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
Run Code Online (Sandbox Code Playgroud)
//.... more code...
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
Run Code Online (Sandbox Code Playgroud)
//.... more code...
当我把print_r($ post ['positions']); 我明白了:array(0 => 118,1 => 119)
in ..../form/UserForm.php我创建了multiply元素
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
在验证文件中,代码是:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
Run Code Online (Sandbox Code Playgroud)
每次看到这个错误的原因是什么,以及我如何解决它?
默认情况下选择在 Zend Framework 2 中附加了 InArray 验证器。
如果您要添加新的 -您将拥有两个。
您应该按如下方式禁用默认值:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
Run Code Online (Sandbox Code Playgroud)
并且您应该删除额外的错误消息。请告诉我们这是否对您有帮助。