Zend EmailAddress验证返回多个错误 [英] Zend EmailAddress Validation returning multiple errors
问题描述
当用户输入无效的电子邮件地址时,我无法使Zend_Validate_EmailAddress仅显示1条错误消息.代码是
I am unable to make Zend_Validate_EmailAddress show only 1 error message when the user enter invalid email address. The code is
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email: ')
->addFilter('StringTrim')
->addFilter('StripTags')
->addValidator('EmailAddress',true, array(... error msgs ...))
->addValidator(new Zend_Validate_Db_NoRecordExists(array( ... db + table + col details ... ),true, array(... error msgs ...)))
->setRequired(true);
$this->addElement($email);
并且当用户输入无效的电子邮件(例如user@email
)(不使用tld)时,它会显示多个错误(例如
And when user enter invalid email like user@email
(without the tld) it show multiple errors like
'email' is no valid hostname for email address 'user@email'
'email' does not match the expected structure for a DNS hostname
'email' appears to be a local network name but local network names are not allowed
我不能使用addErrorMessage('...')
,因为我需要为无效的电子邮件和数据库中已经存在的电子邮件显示不同的消息.因此,如何使EmailAddress验证仅返回1条错误消息的任何想法.
I can't use addErrorMessage('...')
as I need to display different message for invalid email and for email already exists in database. So any idea how to make EmailAddress validation return only 1 error message.
推荐答案
对我来说,问题不在于消息对于普通用户而言过于技术性:这实际上是可以通过覆盖各个消息模板来解决的附带问题. .
To me, the problem is not that the messages are overly technical for the average user: that's really a side issue that can be handled by overriding the individual message templates.
对我来说,根本的问题是此验证器固有地返回多条消息,而我们只需要一条消息.
For me, the fundamental issue is that this validator inherently returns multiple messages and we only want a single message.
我一直不得不求助于标准验证器的子类:
I have always had to resort to sub-classing the standard validator:
class PapayaSoft_Validate_EmailAddress extends Zend_Validate_EmailAddress
{
protected $singleErrorMessage = "Email address is invalid";
public function isValid($value)
{
$valid = parent::isValid($value);
if (!$valid) {
$this->_messages = array($this->getSingleErrorMessage());
}
return $valid;
}
public function getSingleErrorMessage()
{
return $this->singleErrorMessage;
}
public function setSingleErrorMessage($singleErrorMessage)
{
$this->singleErrorMessage = $singleErrorMessage;
return $this;
}
}
然后用法如下:
$validator = new PapayaSoft_Validate_Email();
$validator->setSingleErrorMessage('Your email is goofy');
$element->addValidator($validator, true);
或者,使用简写形式,您需要为验证器添加新的命名空间前缀,以便从新的非Zend命名空间中获取短键"EmailAddress".然后:
Alternatively, using the short form, you need to add a new namespace prefix for validators so that the short key "EmailAddress" gets picked up from the new non-Zend namespace. Then:
$element->addValidator('EmailAddress', true, array(
'singleErrorMessage' => 'Your email is goofy',
));
Note: While the question noted by @emaillenin is similar, the accepted answer there does not actually fulfill your requirements. It does set a single error message for the field, but it sounds like you need to have separate messages coming from the two validators (one for email-format, the other for email-already-exists). For that, it seems to me that you need to change the behavior of the EmailAddress
validator itself.
这篇关于Zend EmailAddress验证返回多个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!