Zend EmailAddress Validation returning multiple errors

前端 未结 4 1151
悲&欢浪女
悲&欢浪女 2021-01-15 10:36

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_Elemen         


        
相关标签:
4条回答
  • 2021-01-15 10:49

    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;
    
    0 讨论(0)
  • 2021-01-15 10:51

    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.

    0 讨论(0)
  • 2021-01-15 10:51

    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>") )
    );
    
    0 讨论(0)
  • 2021-01-15 11:05

    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;
        }
    
    0 讨论(0)
提交回复
热议问题