Zend RegEx 验证器的自定义有意义的错误消息 [英] Custom meaningful error message for Zend RegEx Validator
问题描述
我正在验证表单中的文本字段,如下所示:
I'm validating a text field in my form as follows:
$name = new Zend_Form_Element_Text('name');
$name->setLabel('First Name:')
->setRequired(true)
->addFilter(new Zend_Filter_StringTrim())
->addValidator('regex',true,array('/^[(a-zA-Z0-9)]+$/'))
->addErrorMessage('Please enter a valid first name');
我想要完成的是 - 如何显示有意义的错误消息?例如:如果名字是XYZ-",我怎么能在名字中显示-".
What I'm trying to accomplish is - how can i display a meaningful error message? Eg: If first name is 'XYZ-', how can i display '- is not allowed in first name.'
有没有办法可以访问正则表达式失败的字符?你会推荐其他的东西吗?
Is there a way I can access what character the regex is failing for? Would you recommend something else altogether?
我想过编写一个自定义验证器,但正则表达式非常简单,所以我不明白这一点.我在任何地方都找不到有关 zend 'regex' 验证器的体面文档.
I thought about writing a custom validator but the regex is pretty simple, so I don't see the point. I couldn't find a decent documentation for the zend 'regex' validator anywhere.
如果我不覆盖默认错误消息,我会简单地得到类似的信息:';;;hhbhbhb' 与模式 '/^[(a-zA-Z0-9)]+$/' 不匹配 - 我显然不想向用户显示.
If I don't override the default error message, I simple get something like : ';;;hhbhbhb' does not match against pattern '/^[(a-zA-Z0-9)]+$/' - which I obviously don't want to display to the user.
非常感谢您的意见.
推荐答案
如何通俗地告诉用户你的限制是什么?喜欢
How about telling the user in layman's terms what your restrictions are? Like
Error: Only the letters A to Z and numbers are allowed.
(这让我想到了为什么名字可以包含数字的问题...)
(Which leads me to the question why first names can contain numbers...)
这篇关于Zend RegEx 验证器的自定义有意义的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!