Zend 验证器的一条错误消息而不是几条
我有下一个元素:
$email = new Zend_Form_Element_Text('email');
$email->setAttribs(array('class' => 'input-text', 'id' => 'email'))
->setLabel($this->view->translate('Email'))
->setValue(null)
->setRequired(true)
->addValidator(new Zend_Validate_EmailAddress())
->setDecorators($emailMessageDecorators);
如果电子邮件地址中存在多个错误,则会显示一些错误。像这样:
'fff.fgdf' is no valid hostname for email address '[email protected]'
'fff.fgdf' appears to be a DNS hostname but cannot match TLD against known list
'fff.fgdf' appears to be a local network name but local network names are not allowed
我怎样才能只设置1条消息?我尝试过 setMessage(string),但它显示 3 个相同的错误。谢谢。对不起我的英语。和平与爱)
I have the next element:
$email = new Zend_Form_Element_Text('email');
$email->setAttribs(array('class' => 'input-text', 'id' => 'email'))
->setLabel($this->view->translate('Email'))
->setValue(null)
->setRequired(true)
->addValidator(new Zend_Validate_EmailAddress())
->setDecorators($emailMessageDecorators);
If there are more than one mistake in the email address, some errors are displaying. Like this:
'fff.fgdf' is no valid hostname for email address '[email protected]'
'fff.fgdf' appears to be a DNS hostname but cannot match TLD against known list
'fff.fgdf' appears to be a local network name but local network names are not allowed
How can I set only 1 message? I have tryed setMessage(string), but it shows 3 same errors. Thanks. Sorry for my english. Peace & love)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在过去,我必须为此创建一个自定义验证器:
然后使用以下调用:
$email->addValidator(new App_Validate_EmailAddressSimpleMessage());
如果您只想使用与平常相同的语法:
$email->addValidator('EmailAddress');
但让它使用此验证器,那么您可以更改类名/文件名
EmailAddressSimpleMessage
简单地EmailAddress
并使用表单/元素注册前缀App_Validate_
。更好的可能是允许此类接受您想要的消息的可选构造函数参数,但当时我的做法又快又脏。
In the past, I had to make a custom validator for that:
Then called using:
$email->addValidator(new App_Validate_EmailAddressSimpleMessage());
If you just want to use the same syntax as usual:
$email->addValidator('EmailAddress');
but have it use this validator, then you can change the classname/filename from
EmailAddressSimpleMessage
to simplyEmailAddress
and register the prefixApp_Validate_
with the form/elements.Even better would probably be to allow this class to accept an optional constructor parameter for the message you want, but I was going quick-and-dirty at the time.
如果我没记错的话,您可以调用 setErrorMessages() 在表单元素上设置单个错误消息,而不是在每个单独的验证器上调用 setMessages() :
If I recall correctly, you can call setErrorMessages() to set a single error message on the form element, rather than calling setMessages() on each individual validator:
这是一个例子(我希望它有帮助):
This is an example(I hope it helps) :
这是答案,通过使用 $breakChainOnFailure = true:
引用: http://framework.zend.com/manual/1.12/en/zend.form.elements.html#zend.form.elements.validators.errors
Here is the answer, by using $breakChainOnFailure = true:
Cited: http://framework.zend.com/manual/1.12/en/zend.form.elements.html#zend.form.elements.validators.errors
您使用这个例子:
You using this example: