views:

508

answers:

3

I am building a ZendFramework application which as a login form asking for an email address and password - it seemed to make sense to validate the email address before hitting the database with the login attempt, as an invalid email would never lead to a valid hit. Zend_Validate_EmailAddress seemed like the right way to go, but I am having an issue with it generating multiple errors (question at the bottom, after the code).

My form currently has the following

//WPMail_Form_Login::init()
$email = $this->addElement('text', 'email', array(
    'label'=>'Email',
    'required'=>true,
    'filters'=>array('stringtrim'),
    'validators'=>array(array('emailaddress', true, array(
        'messages'=>array(
            'emailAddressInvalidHostname'=>'Your email address is invalid',
            'emailAddressInvalidFormat'=>'Your email address is invalid',
            '...'=>'(repeat for all message templates)'
        )
    ))),
));

In the controller I directly pass the form into the view:

// WPMail_AuthController::loginAction()
$this->view->form = $form;

And in the view, it's directly echo'd:

// views/scripts/auth/login.phtml
<?php echo $this->form ?>

The result is currently something like this:

- Your email address is invalid
- 'asda!!!' does not match the expected structure for a DNS hostname
- 'asda!!!' does not appear to be a valid local network name

What I want want to know is: Is it possible to configure Zend_Validate_EmailAddress in such a way that it only produces a single email-invalid error? By 'configure' I mean, without extending the class and overriding the logic with my own.

TIA.

A: 

Hi,

As those messages are generated by one validator, I do not think it is possible :-(

The Zend_Validate_EmailAddress::isValid method does all the validations, on generates the errors as a whole, it seems.

One "hacky" solution would be to iterate, in your controller, on the errors, and remove all but the first one, for each field that has more than one... But I don't really like the sound of that...


You could, of course, inherit than and modify the default behaviour... But you stated you didn't want to do that, so...


Still, if I'm wrong, and there is a way, I'm very curious about it ;-)

Pascal MARTIN
Actually, the messages were generated by two validators! Zend_Validate_Hostname was being called indirectly by Zend_Validate_Email.
kander
+2  A: 

Check out this tutorial by Pádraic Brady on using Zend_Form.

Specifically the section "Step 4: Handling Error Messages with a Custom Decorator."

Bill Karwin
Do you mean using the translator in step 5 Bill? That still seems to require multiple declarations of your default message for each error type.
simonrjones
No, I mean step 4. You could write a custom error decorator that returns the *first* message from the email validator like you want, instead of imploding all the messages.
Bill Karwin
Great link! Looks like custom decorators would be another way (compared to simonrjones' solution) to get what I want, although it kinda reminds me of Monty Python's 'Mosquito Hunting' sketch... as in, it's probably a bit heavy-handed to achieve this, compared to simonrjones' solution.
kander
+3  A: 

Zend Form Element has various methods you can use to customise messages . It's not terribly clear from the docs but addErrorMessage() sets a single custom error message on failed validation.

Your example would therefore look like:

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email')
   ->setRequired(true)
   ->addFilter('stringtrim')
   ->addValidator('emailAddress', true)
   ->addErrorMessage('Your email address is invalid');
$this->addElement($email);

See http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.validators.errors

simonrjones
Wonderful, thanks! I actually came across that, but somehow assumed that adding an error message would invalidate the form (as in that it's part of when you do your own validation).. glad to see it's this simple!
kander
It can get confusing.. $email->addError($message) will set an error and mark as invalid, but $email->addErrorMessage($message) just appears to set the message, which overrides the default ones, and doesn't set an error!
simonrjones
+1, thanks for the knowledge tip
Ish Kumar