0

我尝试使用表单进行验证,但我无法收到错误消息。

//这是我的代码:

$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',  
            ),  
        ));  
    }  
}  
?>

_

4

3 回答 3

0

您必须指定“验证器”,例如 getInputFilter:

use
Zend\InputFilter\InputFilter,
Zend\InputFilter\Factory as InputFactory,
Zend\InputFilter\InputFilterAwareInterface,
Zend\InputFilter\InputFilterInterface;

class User implements InputFilterAwareInterface
{
public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();

        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'email',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 1,
                        'max'      => 100,
                    ),
                ),
            ),
        )));
      $this->inputFilter = $inputFilter;        
    }

    return $this->inputFilter;
}
于 2012-08-23T13:16:01.000 回答
0

我还没有使用过 ZF2,但尝试将验证器添加到您的元素中:

use  Zend\Validator;
$this->add(array(  
    'name' => 'id',  
    'attributes' => array(  
        'type'  => 'text',  
        'label' => 'Your name',  
    ),
    'validator' => new StringLength(array('max'=>20))
));  
}  
于 2012-08-14T17:22:47.177 回答
0

您可以通过从模型中检索输入过滤器然后将其设置为表单来做到这一点:

...
$form = new ItemForm();
$form->setInputFilter($user->getInputFilter());
$form->setData($params);
if ($form->isValid()) {
  ...
}
...
于 2012-11-15T14:36:14.200 回答