Zend_Validate_Abstract 自定义验证器不显示正确的错误消息
我的表单中有两个文本字段,我需要确保它们既没有空值也不包含相同的字符串。
我编写的自定义验证器扩展了 Zend_Validate_Abstract 并正常工作,因为它传回了正确的错误消息。在这种情况下:isEmpty 或isMatch。
但是,文档说使用 addErrorMessages 来定义要显示的正确错误消息。
在这种情况下,我已附加
->addErrorMessages(array("isEmpty"=>"foo", "isMatch"=>"bar"));
到表单字段。
根据我读过的所有内容,如果我从 isValid() 返回“isEmpty”,我的错误消息应为“foo”,如果我返回“isMatch”,则它应为“bar”。
但这不是我遇到的情况。如果我从 is valid 返回 false,无论我将 $this->_error() 设置为什么,我的错误消息都会显示“foo”,或者错误消息数组的索引[0]处的任何内容。
如果我没有定义 errorMessages,那么我只会获取为显示传回的错误代码,并根据我传回的内容得到正确的错误代码。
如何捕获错误代码并在表单中显示正确的错误消息?
在我正确解决之前,我已经实现的修复是将完整消息作为错误代码从自定义验证器传回。这在这种情况下是有效的,但错误消息是特定于该页面的,并且实际上不允许重复使用代码。
我已经尝试过的事情: 我已经尝试过验证器链接,以便我的自定义验证器仅检查匹配项:
->setRequired("true")
->addValidator("NotEmpty")
->addErrorMessage("URL May Not Be Empty")
->addValidator([*customValidator]*)
->addErrorMessage("X and Y urls may not be the same")
但同样,如果其中任何一个抛出错误,则显示要设置的最后一个错误消息,无论错误真正是什么。
我不完全确定从这里该去哪里。
有什么建议吗?
I have two text fields in a form that I need to make sure neither have empty values nor contain the same string.
The custom validator that I wrote extends Zend_Validate_Abstract and works correctly in that it passes back the correct error messages. In this case either: isEmpty or isMatch.
However, the documentation says to use addErrorMessages to define the correct error messages to be displayed.
in this case, i have attached
->addErrorMessages(array("isEmpty"=>"foo", "isMatch"=>"bar"));
to the form field.
According to everything I've read, if I return "isEmpty" from isValid(), my error message should read "foo" and if i return "isMatch" then it should read "bar".
This is not the case I'm running into though. If I return false from is valid, no matter what i set $this->_error() to be, my error message displays "foo", or whatever I have at index[0] of the error messages array.
If I don't define errorMessages, then I just get the error code I passed back for the display and I get the proper one, depending on what I passed back.
How do I catch the error code and display the correct error message in my form?
The fix I have implemented, until I figure it out properly, is to pass back the full message as the errorcode from the custom validator. This will work in this instance, but the error message is specific to this page and doesn't really allow for re-use of code.
Things I have already tried:
I have already tried validator chaining so that my custom validator only checks for matches:
->setRequired("true")
->addValidator("NotEmpty")
->addErrorMessage("URL May Not Be Empty")
->addValidator([*customValidator]*)
->addErrorMessage("X and Y urls may not be the same")
But again, if either throws an error, the last error message to be set displays, regardless of what the error truly is.
I'm not entirely sure where to go from here.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你误解了手册。 它说
这些函数将自定义错误消息添加到整个表单堆栈中。
如果您想在验证失败时显示验证错误消息,则必须在验证器内实现该消息。
IE。
这样,验证失败后,可以使用
$validator->getErrors()
获取错误码,使用$validator->getMessages()
获取错误消息。如果您正确定义了
$_messageTemplates
,Zend_Form
会自动使用错误消息而不是错误代码并将其打印出来。希望这有帮助。
I think you misinterpreted the manual. It says
These functions add custom error messages to the whole form stack.
If you want to display validation error messages when the validation fails, you have to implement the message inside your validator.
ie.
This way, after the validation fails, you can get the error codes using
$validator->getErrors()
and the error messages using$validator->getMessages()
.If you have the
$_messageTemplates
properly defined,Zend_Form
automatically uses the error messages instead of error codes and prints them out.Hope this helps.