Zend EmailAddress 验证返回多个错误 [英] Zend EmailAddress Validation returning multiple errors

查看:16
本文介绍了Zend EmailAddress 验证返回多个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入无效的电子邮件地址时,我无法让 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',
));

注意:虽然 @emaillenin 指出的问题 类似,那里接受的答案实际上并不能满足您的要求.它确实为 字段 设置了一条错误消息,但听起来您需要有来自两个验证器的单独 消息(一个用于电子邮件格式,另一个用于电子邮件已经存在).为此,在我看来您需要更改 EmailAddress 验证器本身的行为.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆