Creating a zend form validator EmailAddress returns only one custom error message

I am creating an email form element similar to this (inside a Zend form):

//create e-mail element $email = $this->createElement('text', 'username') ->setLabel('E-mail:') ->setRequired(true) ->addFilters(array('StringTrim', 'StringToLower')) ->addValidator('EmailAddress', false, array( 'messages' => array( Zend_Validate_EmailAddress::INVALID => 'Dit e-mail adres is ongeldig.', ) )); //add element $this->addElement($email); 

Now that you have entered the wrong e-mail, a lot of messages appear:

 '#' is no valid hostname for email address '@# $@ #' '#' does not match the expected structure for a DNS hostname '#' does not appear to be a valid local network name '@#$' can not be matched against dot-atom format '@#$' can not be matched against quoted-string format '@#$' is no valid local part for email address '@# $@ #' 

I wonder if it’s possible for him to only display the error message provided by me, for example, β€œPlease enter a valid email address.”

+4
source share
2 answers

The easiest way is to use addErrorMessage() to set up one custom message for all errors.

In your example, you can add it to your code and call it freely, or add a line

 $email->addErrorMessage('Dit e-mail adres is ongeldig.'); 

You should also change the second addValidator parameter as shown below, so that after the validation fails, the other conditions are not validated.

 ->addValidator('EmailAddress', true) 

This is explained in the Reference Guide , but not very well. The name addErrorMessage does not mean that it will cancel the default messages, but there you are.

+5
source

You can use either:

 $email->addErrorMessage('Dit e-mail adres is ongeldig.'); $email->addValidator('EmailAddress', true); 

Or you can use:

 $email_validate = new Zend_Validate_EmailAddress(); $email_validate->setMessage("Your custom message"); $email->addValidator($email_validate ,TRUE); 
+1
source

All Articles