5

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....

Vinay
  • 267
  • 1
  • 3
  • 14

3 Answers3

4

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......

Daniel
  • 27,718
  • 20
  • 89
  • 133
Pushpendra
  • 4,344
  • 5
  • 36
  • 64
  • This is good, but what if you have multiple validators, each with a custom message? Adding this one message would override all the other error messages from the other validators. – The Unknown Dev Dec 16 '14 at 23:01
1

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.

Maxence
  • 12,868
  • 5
  • 57
  • 69
0

I see that you are passing your own Custom Validator.

->addValidator(new Validator_EmailValidator())

You don't need to do that. Just use :

 $validator =  new Zend_Validate_EmailAddress()

Then just set that validator on the form item, and then set the messages against that validator.

So

 $emailId->setValidator( $validator );

Now just set the Messages against the Validator, using the setMessages method.

These are all of the potential messages that you can change:

    const INVALID            = 'emailAddressInvalid';
    const INVALID_FORMAT     = 'emailAddressInvalidFormat';
    const INVALID_HOSTNAME   = 'emailAddressInvalidHostname';
    const INVALID_MX_RECORD  = 'emailAddressInvalidMxRecord';
    const INVALID_SEGMENT    = 'emailAddressInvalidSegment';
    const DOT_ATOM           = 'emailAddressDotAtom';
    const QUOTED_STRING      = 'emailAddressQuotedString';
    const INVALID_LOCAL_PART = 'emailAddressInvalidLocalPart';
    const LENGTH_EXCEEDED    = 'emailAddressLengthExceeded';

Message Defaults

protected $_messageTemplates = array(
        self::INVALID            => "Invalid type given. String expected",
        self::INVALID_FORMAT     => "'%value%' is no valid email address in the basic format local-part@hostname",
        self::INVALID_HOSTNAME   => "'%hostname%' is no valid hostname for email address '%value%'",
        self::INVALID_MX_RECORD  => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'",
        self::INVALID_SEGMENT    => "'%hostname%' is not in a routable network segment. The email address '%value%' should not be resolved from public network",
        self::DOT_ATOM           => "'%localPart%' can not be matched against dot-atom format",
        self::QUOTED_STRING      => "'%localPart%' can not be matched against quoted-string format",
        self::INVALID_LOCAL_PART => "'%localPart%' is no valid local part for email address '%value%'",
        self::LENGTH_EXCEEDED    => "'%value%' exceeds the allowed length",
    );

Now just change the messages to whatever you want. You will need to update every message.

 $validator->setMessages(array(
    Zend_Validate_EmailAddress::INVALID            => "Invalid type given, value should be a string",
    Zend_Validate_EmailAddress::INVALID_FORMAT     => "'%value%' is no valid email address in the basic format local-part@hostname",
    Zend_Validate_EmailAddress::INVALID_HOSTNAME   => "'%hostname%' is no valid hostname for email address '%value%'",
    Zend_Validate_EmailAddress::INVALID_MX_RECORD  => "'%hostname%' does not appear to have a valid MX record for the email address '%value%'",
    Zend_Validate_EmailAddress::INVALID_SEGMENT    => "'%hostname%' is not in a routable network segment. The email address '%value%' should not be resolved from public network.",
    Zend_Validate_EmailAddress::DOT_ATOM           => "'%localPart%' can not be matched against dot-atom format",
    Zend_Validate_EmailAddress::QUOTED_STRING      => "'%localPart%' can not be matched against quoted-string format",
    Zend_Validate_EmailAddress::INVALID_LOCAL_PART => "'%localPart%' is no valid local part for email address '%value%'",
    Zend_Validate_EmailAddress::LENGTH_EXCEEDED    => "'%value%' exceeds the allowed length",
  ));
Layke
  • 51,422
  • 11
  • 85
  • 111
  • I tried that already but the messages like second and third shown in the image above are still coming....? – Vinay Sep 01 '11 at 13:26