从控制器设置 Zend\Form 错误消息 [英] Set Zend\Form Error Messages from Controller
问题描述
这可能是一项非常简单的任务,但目前我在这方面做得很糟糕.当我的身份验证失败时,我只想在我的表单中添加一个自定义错误.
this is probably a very simple task, but currently I'm failing horribly at it. I just want to add a custom error to my form when my authentication fails.
我尝试了什么
$form->setMessages(array(
array('password' => $this->failedLoginMessage)
));
意外结果
\Zend\Debug\Debug::dump($form->getMessages());
array(0) {}
如果我正确理解了代码,这应该将错误消息附加到密码元素.实际上查看 setMessages 我认为附加一个单维数组应该已经足够了,但它也需要第二维:S我只是坚持那个简单的任务,叹息:)
If i understand the code correctly this should attach an error message to the password element. Actually looking at the setMessages i thought attaching a single-dimension array should have been enough, but it needs the 2nd dimension, too :S I'm just stuck on that simple task, sigh :)
提前致谢!
推荐答案
我的评论作为答案,正如您所要求的:
My comment as answer, as you've requested:
您也可以直接为元素设置错误消息,使用:
You can also set error messages to an element directly, using:
$form->get('elemName')->setMessages(array('message1', 'message2', ...));
这篇关于从控制器设置 Zend\Form 错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!