Zend EmailAddress验证返回多个错误

时间:2011-10-30 01:00:14

标签: zend-framework zend-form zend-form-element zend-validate

当用户输入无效的电子邮件地址时,我无法使Zend_Validate_EmailAddress只显示1条错误消息。代码是

$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)时,会显示多个错误,例如

'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条错误消息。

4 个答案:

答案 0 :(得分:7)

对我来说,问题不在于消息对于普通用户而言过于技术化:这实际上是一个可以通过覆盖单个消息模板来处理的副问题。

对我而言,根本问题在于此验证器本身会返回多个消息,而我们只需要一条消息。

我总是不得不求助于对标准验证器进行子类化:

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”。然后:

$element->addValidator('EmailAddress', true, array(
    'singleErrorMessage' => 'Your email is goofy',
));

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

答案 1 :(得分:2)

你可以将这一行放在isValid()函数的末尾,但在返回false之前。

$this->_messages = array($this->_messageTemplates[self::INVALID]); 
像这样:

public function isValid($value)
    {
        if (!is_string($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $matches = array();
        $length  = true;
        $this->_setValue($value);

        // Split email address up and disallow '..'
        if ((strpos($value, '..') !== false) or
            (!preg_match('/^(.+)@([^@]+)$/', $value, $matches))) {
            $this->_error(self::INVALID_FORMAT);
            return false;
        }

        $this->_localPart = $matches[1];
        $this->_hostname  = $matches[2];

        if ((strlen($this->_localPart) > 64) || (strlen($this->_hostname) > 255)) {
            $length = false;
            $this->_error(self::LENGTH_EXCEEDED);
        }

        // Match hostname part
        if ($this->_options['domain']) {
            $hostname = $this->_validateHostnamePart();
        }

        $local = $this->_validateLocalPart();

        // If both parts valid, return true
        if ($local && $length) {
            if (($this->_options['domain'] && $hostname) || !$this->_options['domain']) {
                return true;
            }
        }

         $this->_messages = array($this->_messageTemplates[self::INVALID]); // ===========By Jagdish Ram JPK --DS-----------

        return false;
    }

答案 2 :(得分:1)

你必须像这样编写验证器......

$email->addValidator(
    'EmailAddress', 
    true, 
    array( 'messages' => array( 'emailAddressInvalidFormat' => "Email Address is Not Valid... !<br>", "emailAddressInvalidHostname"=>"Email Address is Not Valid... !<br>", "hostnameUnknownTld"=>"Email Address is Not Valid... !<br>","hostnameLocalNameNotAllowed"=>"Email Address is Not Valid... !<br>") )
);

答案 3 :(得分:0)

试试这个,

$po = $this->getRequest()->getPost();
$email = $po['email'];
$emval = new Zend_Validate_EmailAddress();
if (!$emval->isValid($email)) {
$this->view->emailerror = "Please enter a valid email ID.";
}

在其视图页面中,将代码放在要显示该错误消息的位置下方,

       echo $this->emailerror;