views:

36

answers:

1

Hi all,

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.

A: 

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

http://stackoverflow.com/questions/3844835/zend-validators-and-error-messages-addvalidator-and-adderrormessage/3846829#3846829

In short, try this:

'messages' => 'Email already exists'

instead of using an array.

Adrian Schneider
Hi adrian I've updted my question with the results I got based on your post
javiervd
Looks like the messages = string method does not work, because EmailAddress validator overrides it in order to proxy some of the validation to the Hostname validator. Works for all (most?) of the other validators though.
Adrian Schneider