Set one error message for email field in Zend

I ran into a little problem with checking email in my Zend form.

In the field "My code for e-mail" is indicated

$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.'
                              )
                           )
                        );

It currently shows email error messages: enter image description here

I want to install a single error message instead of all these errors and this is how:

"'abcd @shdsjah' is not a valid email id.

Since I am new to the Zend Framework, I do not really understand about it, although I tried some code, but they are useless.

Please, help.....

Thanks in advance...

+5
source share
3

zend-framework, , setErrors(), :

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

:

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

.

, ......

+5

true addValidator (breakChainOnFailure). ​​ , .

+1

, .

->addValidator(new Validator_EmailValidator())

. :

 $validator =  new Zend_Validate_EmailAddress()

, .

,

 $emailId->setValidator( $validator );

Validator, setMessages.

, :

    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';

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 message to whatever you want. You will need to update each 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",
  ));
0
source

All Articles