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.
You can put this one line at end of isValid() funciton but before return false.
like this:
Try this,
And in its view page put the code below where you want to display that error message,
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:
Then usage is as follows:
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:
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.you have to write validator like this...