Zend EmailAddress Validation returning multiple errors

回眸只為那壹抹淺笑 提交于 2019-12-01 08:25:52

问题


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);

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  

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.


回答1:


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;
    }
}

Then usage is as follows:

$validator = new PapayaSoft_Validate_Email();
$validator->setSingleErrorMessage('Your email is goofy');
$element->addValidator($validator, true);

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.




回答2:


You can put this one line at end of isValid() funciton but before return false.

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

like this:

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;
    }



回答3:


you have to write validator like this...

$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>") )
);



回答4:


Try this,

$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.";
}

And in its view page put the code below where you want to display that error message,

       echo $this->emailerror;


来源:https://stackoverflow.com/questions/7942674/zend-emailaddress-validation-returning-multiple-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!