php Zend Form:在表单验证后添加错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2500097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zend Form: add error message after form validation
提问by takeshin
How to add an error message to Zend Form element after the form was already validated?
如何在表单已经验证后向 Zend Form 元素添加错误消息?
I'm trying to add error mesages I get from Zend_Auth(now I'm displaying them using flashMessenger).
我正在尝试添加我从中获得的错误消息Zend_Auth(现在我正在使用 flashMessenger 显示它们)。
I tried something like this:
我试过这样的事情:
$form->getElement('username')->addErrorMessage('my message');
采纳答案by thetaiko
From the zend form documentation -
从 Zend 表单文档 -
addErrorMessage($message): add an error message to display on form validation errors. You may call this more than once, and new messages are appended to the stack.
addError($message): add a message to the custom error messages stack and flag the form as invalid.
addErrorMessage($message):添加错误消息以显示表单验证错误。您可以多次调用它,并且新消息会附加到堆栈中。
addError($message):向自定义错误消息堆栈添加一条消息并将表单标记为无效。
If your form is not marked as invalid, it probably doesn't show any error messages. Using addError($message)rather than addErrorMessage($message)will ensure that the element is also marked invalid.
如果您的表单未标记为无效,则它可能不会显示任何错误消息。使用addError($message)而不是addErrorMessage($message)将确保元素也被标记为无效。
回答by Andrei Stalbe
if(!$your_zend_auth_result){
$form->getElement('username')->addError('Your Message');
$form->markAsError();
}
回答by gromaco
You need to use setErrors()method to create errors stack.
In the case, when element already has some errors you should use addErrors()method.
To check if element has errors you can use hasErrors()method
您需要使用setErrors()方法来创建错误堆栈。在这种情况下,当元素已经有一些错误时,您应该使用addErrors()方法。要检查元素是否有错误,您可以使用hasErrors()方法

