views:

143

answers:

1

This is strange. I'm trying to set the error message for my email validation element in Zend form. Other fields are working correctly-but not email-what's up?

code bit:

$name = new Zend_Form_Element_Text('name');
        $name   ->setLabel('Name:')
                ->setRequired(true)
                ->addValidator('Alnum')
                ->addValidator('NotEmpty');
        $name    ->getValidator('NotEmpty')->setMessage('Please enter your name.');
        $name    ->getValidator('Alnum')->setMessage('Name can only contain letters and spaces.');

$email = new Zend_Form_Element_Text('email');
       $email   ->setLabel('Email:')
             ->setRequired(true)
                ->addFilter('StringToLower')
                ->addValidator('NotEmpty')
                ->addValidator('EmailAddress');

        $email  ->getValidator('NotEmpty')->setMessage('Please enter your email address.');
        $email  ->getValidator('EmailAddress')->setMessage('Email is not in proper format.');

Name works, but email doesn't work.

I am still getting the defauly error message: '' is no valid email address in the basic format local-part@hostname

+1  A: 

You need to specify the message type:

$email->getValidator('emailAddress')->setMessage("'%value%' is not valid, use something like local-part@hostname",Zend_Validate_EmailAddress::INVALID_FORMAT);

Take this as example:

protected $_messageTemplates = array(
        self::INVALID            => "Invalid type given, value should be a string",
        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",
    );
Keyne
Is there an easy way to make an error message that will work for all those errors? IE: "There is a problem with your email address."Thanks!
Joel
I think you can extend the emailAddress validator and change the protected variable $_messageTemplates so that you even don't need to set the message.http://stackoverflow.com/questions/1811085/zend-framework-custom-validation-classeshttp://framework.zend.com/manual/en/zend.validate.writing_validators.html
Keyne