我尝试使用表单进行验证,但我无法收到错误消息。
//这是我的代码:
$form = new TestForm();
$form->setInputFilter(new TestFilter());
$data = array('id'=>'','email'=>'myemail@myemail.com');
$form->setData($data);
if($form->isValid()){
echo 'ok';
} else {
echo 'not ok <br/>';
$messagesForm = $form->getMessages();
$filter=$form->getInputFilter();
$messagesFilter=$filter->getMessages();
var_dump($messagesForm);
var_dump($messagesFilter);
}
/////////////////
Output
not ok
//messagesForm
array
empty
//MessagesFilter
array
'id' =>
array
'isEmpty' => string 'Value is required and can't be empty' (length=36)
_
怎么可能?过滤器没问题,但我无法从表单中收到错误消息可能是错误或我做错了什么?
完整代码:
测试过滤器:_
<?php
namespace mvc\filter;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
class TestFilter extends InputFilter
{
public function __construct()
{
$factory = new InputFactory();
$this->add($factory->createInput(array('name'=>'id','required'=>true)));
$this->add($factory->createInput(array('name'=>'email','required'=>true)));
}
}
?>
_
TestForm _
命名空间 mvc\form;
使用 Zend\InputFilter\Factory;
使用 Zend\Form\Element;
使用 Zend\Form\Form;
class TestForm extends Form
{
public function prepareElements()
{
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'text',
'label' => 'Your name',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'email',
'label' => 'Your email address',
),
));
}
}
?>
_