Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend EmailAddress Validation returning multiple errors

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.

like image 328
Bryan Avatar asked Dec 30 '25 14:12

Bryan


1 Answers

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.

like image 69
David Weinraub Avatar answered Jan 02 '26 10:01

David Weinraub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!