一个错误消息,而不是很少的Zend验证器 [英] One error message instead of few for Zend validator
问题描述
我有下一个元素:
$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 'as@fff.fgdf'
'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个相同的错误。谢谢。对不起我的英语不好。和平与安宁爱情)
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)
推荐答案
过去,我不得不为此做出一个自定义验证器:
In the past, I had to make a custom validator for that:
/**
* The standard email address validator with a single, simple message
*/
class App_Validate_EmailAddressSimpleMessage extends Zend_Validate_EmailAddress
{
const COMMON_MESSAGE = 'Invalid email address';
protected $_messageTemplates = array(
self::INVALID => self::COMMON_MESSAGE,
self::INVALID_FORMAT => self::COMMON_MESSAGE,
self::INVALID_HOSTNAME => self::COMMON_MESSAGE,
self::INVALID_MX_RECORD => self::COMMON_MESSAGE,
self::INVALID_SEGMENT => self::COMMON_MESSAGE,
self::DOT_ATOM => self::COMMON_MESSAGE,
self::QUOTED_STRING => self::COMMON_MESSAGE,
self::INVALID_LOCAL_PART => self::COMMON_MESSAGE,
self::LENGTH_EXCEEDED => self::COMMON_MESSAGE,
);
}
然后调用:
$ email-> addValidator(new App_Validate_EmailAddressSimpleMessage());
如果你只需要像往常一样使用相同的语法:
If you just want to use the same syntax as usual:
$ email-> addValidator('EmailAddress');
,但使用此验证器,则可以将类名/文件名从 EmailAddressSimpleMessage
更改为简单 EmailAddress
并使用表单/元素注册前缀 App_Validate _
。
but have it use this validator, then you can change the classname/filename from EmailAddressSimpleMessage
to simply EmailAddress
and register the prefix App_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.
这篇关于一个错误消息,而不是很少的Zend验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!