ZF2表单验证是空的验证总是失败

时间:2014-09-08 06:19:29

标签: php forms validation zend-framework2

HIHO,

我的ZF2表格有问题。 每次我提交它时都会出现以下错误:

(result of $form->isValid() and var_dump($form->getMessages());

array (size=1)
'imagecode' => 
  array (size=1)
    'isEmpty' => string 'Value is required and can't be empty' (length=36)

以下是'imagecode' - formfieldcode:

public function __construct($name = null)
{
    parent::__construct('advert');

    $this->setAttribute('method', 'post');

    $this->add(array(
        'name' => 'imagecode',
        'type' => 'Zend\Form\Element\Textarea',
        'attributes' => array(
            'required' => 'required',
        ),
        'options' => array(
            'label' => 'Bannercode:'
        ),
    ));

验证员:

public function getInputFilter()
{
    if (!$this->_inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();

...

        $inputFilter->add($factory->createInput(array(
            'name'       => 'imagecode',
            'required'   => true,
            'filters'    => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'min'      => '3',
                        'max'      => '5000',
                    ),
                ),
            ),
        )));

        $this->_inputFilter = $inputFilter;
    }

    return $this->_inputFilter;
}

其他字段工作正确且经过验证是正确的,但不是Textarea。

最后是ControllerCode:

$advert  = $service->getAdvertById($id);
        $form    = $service->getAdvertForm();
        $request = $this->getRequest();

        $form->bind($advert);

        if ($request->isPost()) {
            $filter = new AdvertFilter();
            $form->setData($request->getPost());

            $form->setInputFilter($filter->getInputFilter());

此后验证失败,我不知道为什么。 我希望任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

看看这一行:

'required' => 'required,

和.... 改为:

'required' => 'required', 

答案 1 :(得分:0)

我已经弄清楚了。 我试图输入html代码以便

array('name' => 'StripTags')

删除了它。

有时它很简单._。'