Zend框架验证,值存在但仍然错误

时间:2019-04-13 23:17:44

标签: php zend-framework

我有一个联系表单,该表单似乎无法验证,但是存在正确的输入数据。

我已经按照官方教程中的说明进行操作,但是仍然出现此错误,而帖子数据中似乎存在所有必填字段。

下面是我的表单和控制器定义:

class Contact extends Form
{
    public function __construct($name = null, $options = [])
    {
        parent::__construct($name, $options);

        $input = new Input("from");
        $input->getFilterChain()
            ->attach(new StringTrim())
            ->attach(new StringToLower());
        $input->getValidatorChain()
            ->attach(new NotEmpty());

        $inputFilter = new InputFilter();
        $inputFilter->add($input);

        $this->getInputFilter()->add($inputFilter);

        $this->add(new Text(
            'subject', [
                'label'=>'Subject'
            ]
        ));

        $this->add(new Textarea(
            'message', [
                'label'=>'Message'
            ]
        ));

        $this->add(new Text(
            'from', [
                'label'=>'From'
            ]
        ));

        $button = new Submit();
        $button->setName('submit');
        $button->setLabel('Versturen');
        $button->setAttribute('value', 'Versturen');
        $this->add($button);
    }
}

//My controller definition 
class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $form = new Contact();
        if($this->getRequest()->isPost())
        {
            $data = $this->params()->fromPost();

            echo "<pre>";
            $form->setData($data);
            var_dump($form->isValid(),
                $form->getInputFilter()->getMessages());

            var_dump($data);
            exit;
        }

        return new ViewModel([
            'form'=>$form
        ]);
    }
}

以下数据和验证消息:

//The post-data is as follows:
array(4) {
  ["subject"]=>
  string(10) "wafefewfwf"
  ["from"]=>
  string(10) "waefwfewfw"
  ["message"]=>
  string(0) ""
  ["submit"]=>
  string(9) "Versturen"
}

//But It still gives me the following errors:
array(1) {
  [""]=>
  array(1) {
    ["from"]=>
    array(1) {
      ["isEmpty"]=>
      string(36) "Value is required and can't be empty"
    }
  }
}

0 个答案:

没有答案