1
votes

I used to have this form element to validate an email and display an error message if the format was invalid:

  $email_users = new Zend_Form_Element_Text('email_users');
  $email_users->setLabel('Email:')
                      ->setRequired(false)
                      ->addFilter('StripTags')
                      ->addFilter('StringTrim')
                      ->addValidator('EmailAddress')
                      ->setErrorMessages(array('messages' => 'Invalid Email'));

setErrorMessages worked fine because this was the only validation I needed so it replaced all error messages with my custom one, now I had to add another validation to see if it already existed in my DB:

  $email_users = new Zend_Form_Element_Text('email_users');
  $email_users->setLabel('Email:')
                      ->setRequired(false)
                      ->addFilter('StripTags')
                      ->addFilter('StringTrim')
                      ->addValidators(array(
                          array('EmailAddress', true,
                              array(
                                  'messages' =>
                                      array(Zend_Validate_EmailAddress::INVALID => 'Invalid Email')
                                  )
                          ),
                          array('Db_NoRecordExists', true,
                              array(
                                  'messages' =>
                                    array(Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'),
                                  'table' => 'users',
                                  'field' => 'email_users')
                          )));

The functionality is fine, the problem is that when the email is invalid it now shows me the default zend validate messages, ut when it exists it does show me my custom message. Is there any way to archive the previous functionality this way? (Replacing all invalid email messages) I can't uset setErrorMessages since this shows me 'invalid email' when the email alrady exists.

I tried using 'messages' => 'Error' but nothing happens (no errors but the default messages show), I tried:

$emailValidator = new Zend_Validate_EmailAddress(); $emailValidator->setMessages('Invalid email');

And on my form element I added

$email_users->addValidator($emailValidator)

Nothing same results. The closest I have gotten is doing 'messages' => array(Zend_Validate_EmailAddress::INVALID_FORMAT => 'Invalid email') this shows the msg when I type something like 'email@' or 'email' but if I type 'email@host' it shows me 3 erros regarding the hostname, dns and localnetwork, which they don't show when I use setMessages('Error') (just displays 'Error'),

Thanks in advance.

2

2 Answers

0
votes

I posted an answer which explains how all the different error message setting functions work here,

Zend validators and error messages: addValidator and addErrorMessage

In short, try this:

'messages' => 'Email already exists'

instead of using an array.

0
votes

You have to write validator like this..

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

In all cases of invalid email address error must show "Email Address is Not Valid... !".