Zend EmailAddress Validation returning multiple errors

点点圈 提交于 2019-12-01 10:44:04
David Weinraub

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.

Jagdish Ram

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

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

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