Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a single error message for a Email field in Zend

I am facing an small issue regarding the Email Validation in my Zend Form.

My Code for the Email field is as

$emailId = new Zend_Form_Element_Text('email');
$emailId->setLabel("Email Adresse")
         ->addFilter('StripTags')
         ->addFilter('StringTrim')
         ->addValidator(new Validator_EmailValidator())
         ->addValidator('NotEmpty')
         ->addValidator(
                        'NotEmpty',
                        TRUE,
                        array('messages' => array(
                              'isEmpty' => 'Please enter your email id.'
                              )
                           )
                        );

Currently it is showing the Email Error Messages as : enter image description here

What I want is to set a single error message in the place of all these errors and that is as :

"'abcd@shdsjah' is not a valid Email Id."

Since I am new to the Zend Framework, I don't have much idea about it, although I tried some code but they are useless.

Please help.....

Thanks In Advance....

like image 507
Vinay Avatar asked Sep 01 '11 11:09

Vinay


2 Answers

When I was new to zend-framework, I faced this problem and got solution by using setErrors() method as:

//this will immediately call the method markAsError() which will show the error always
$emailId->setErrors(array('Please enter a valid Email Id.'));

You can also try :

//this will clearErrorMessages() and after that set the error messages 
$emailId->setErrorMessages(array("Please enter a valid Email Id."));

Write this code after your code.

I hope it will be helpful to you......

like image 69
Pushpendra Avatar answered Sep 27 '22 19:09

Pushpendra


Pass true as second argument of addValidator (breakChainOnFailure). The validation will stop at the first failure and you will have only have one error message.

like image 43
Maxence Avatar answered Sep 27 '22 19:09

Maxence