Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How can I get just 1 error message for EmailAddress validator instead of multiple?

  1. #1
    ZendLearner is offline Junior Member
    Join Date
    Mar 2009
    Posts
    26

    Default How can I get just 1 error message for EmailAddress validator instead of multiple?

    Hi, I have created a text form element and added the EmailAddress validator and set 'breakChainOnFailure' to true but I am getting multiple error messages.

    Here is my code snippet:

    // add text element
    $this->addElement('text', 'email', array(
    'label' => 'Email',
    'description' => 'Please enter valid email',
    'required' => true,
    'filters' => array('StringTrim'),
    'validators' => array(
    array('validator' => 'EmailAddress', 'breakChainOnFailure' => true)
    )
    ));

    Here are the errors I get when I enter an invalid email address (ex: asdf@asfsadf):

    1. 'asfsadf' is not a valid hostname for email address 'asdf@asfsadf'
    2. 'asfsadf' does not match the expected structure for a DNS hostname
    3. 'asfsadf' appears to be a local network name but local network names are not allowed


    I would only like the very first error message to display and I thought 'breakChainOnFailure' => true would do that. Does anybody know what I am doing wrong? Any help would be appreciated.

  2. #2
    windman is offline Junior Member
    Join Date
    Mar 2009
    Posts
    9

    Default

    emailAddress validator has 7 message teplates;

    [PHP]
    class Zend_Validate_EmailAddress extends Zend_Validate_Abstract
    {
    const INVALID = 'emailAddressInvalid';
    const INVALID_HOSTNAME = 'emailAddressInvalidHostname';
    const INVALID_MX_RECORD = 'emailAddressInvalidMxRecord';
    const DOT_ATOM = 'emailAddressDotAtom';
    const QUOTED_STRING = 'emailAddressQuotedString';
    const INVALID_LOCAL_PART = 'emailAddressInvalidLocalPart';
    const LENGTH_EXCEEDED = 'emailAddressLengthExceeded';

    protected $_messageTemplates = array(
    self::INVALID => "'%value%' is not a valid email address in the basic format local-part@hostname",
    self::INVALID_HOSTNAME => "'%hostname%' is not a 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:OT_ATOM => "'%localPart%' not matched against dot-atom format",
    self::QUOTED_STRING => "'%localPart%' not matched against quoted-string format",
    self::INVALID_LOCAL_PART => "'%localPart%' is not a valid local part for email address '%value%'",
    self::LENGTH_EXCEEDED => "'%value%' exceeds the allowed length"
    );

    ...
    }
    [/PHP]

    so you have to specify which message to display. For example if you want to display only INVALID_HOSTNAME, LENGTH_EXCEEDED and DOT_ATOM messages but disable LENGTH_EXCEEDED and DOT_ATOM messages if INVALID_HOSTNAME occurs then you have to do like this;
    [PHP]
    $element->addValidator('EmailAddress', true, array('messages' => array(
    'emailAddressInvalidHostname' => 'hostname is
    invalid!')))
    $element->addValidator('EmailAddress', false, array('messages' => array(
    'emailAddressLengthExceeded' => 'length is too long!',
    'emailAddressDotAtom' => 'dot problem!')))
    [/PHP]

    i did not try this but it seems to work

  3. #3
    ZendLearner is offline Junior Member
    Join Date
    Mar 2009
    Posts
    26

    Default I tried your suggestion, but it did not work

    Hi, thank you for your response. I tried to do as you said, but it is still displaying more than 1 error message. Here is the code snippet:

    // add text element
    $this->addElement('text', 'email', array(
    'label' => 'Email',
    'description' => 'Please enter valid email',
    'required' => true,
    'filters' => array('StringTrim')
    ));
    $this->getElement('email')
    ->addValidator('EmailAddress', true, array(
    'messages' => array('emailAddressInvalidHostname' => 'hostname is invalid!')
    ))
    ->addValidator('EmailAddress', false, array(
    'messages' => array('emailAddressLengthExceeded' => 'length is too long!', 'emailAddressDotAtom' => 'dot problem!')
    ));


    After searching around the forums I think the best way would be to Extend Zend_Validate_EmailAddress:

    class My_Validate_CustomEmailAddress extends Zend_Validate_EmailAddress
    {
    public function getMessages() {
    $this->_messages = array("This is not a valid e-mail address.");
    return $this->_messages;
    //OR JUST return array("This is not a valid e-mail address.");
    }
    }

  4. #4
    thomas is offline Senior Member
    Join Date
    Aug 2008
    Posts
    174

    Default

    The second parameter "breakChainOnFailure" is not for setting is a error message is being displayed or not.

    And it's also not to set if it breaks on the first message.

    This parameter defines that no further validator will be processed when set to true.

    So when you have a chain like
    emailValidator->passwortValidator
    the passwordValidator would not be processed when email validates to false.
    Greetings
    Thomas Weidner
    I18N Team Leader, Zend Framework
    http://www.thomasweidner.com

  5. #5
    ZendLearner is offline Junior Member
    Join Date
    Mar 2009
    Posts
    26

    Default true

    Quote Originally Posted by thomas View Post
    This parameter defines that no further validator will be processed when set to true.

    Hi,
    You are correct that no further validator(s) will be processed when breakchainonfailure is set to true. Unfortunately, that doesn't help me solve the problem of getting just 1 error message from the EmailAddress validator instead of possibly multiple error messages.

    What I ultimately wanted was to get the error messages and render only the very first error message. However, after taking a look at Zend/Validate/EmailAddress.php's isValid() method, I don't think this is possible. However, I did find that it is possible to subclass Zend/Validate/EmailAddress.php and overwrite getMessages() method so that only 1 error message will be displayed when EmailAddress validation fails:

    Code:
    <?php
    class My_Validate_EmailAddress extends Zend_Validate_EmailAddress
    {
    	public function getMessages() {
    		$this->_messages = array("This is not a valid e-mail address.");
    		return $this->_messages;
    		/* OR SIMPLY
    		return array("This is not a valid e-mail address.");
    		*/
    	}
    }

  6. #6
    Bizzer is offline Junior Member
    Join Date
    Sep 2009
    Posts
    3

    Default

    Hello,

    Could anyone solve this yet without hacks? This seems so weird that the most typical usage of Email validator is not implemented. I'm new to ZF... maybe anyone knows if new versions support that? I've searched the web for a couple of hours and still don't see an answer. Don't you guys use email validation??

    I'm about to finish with regexp validation for this.

    Thanks!

  7. #7
    jweber is offline Senior Member
    Join Date
    Jun 2008
    Location
    Florida
    Posts
    167

    Default

    Use the validator's setMessage() method.

  8. #8
    Bizzer is offline Junior Member
    Join Date
    Sep 2009
    Posts
    3

    Default

    Quote Originally Posted by jweber View Post
    Use the validator's setMessage() method.
    This does not work. I found the solution - "addErrorMessage()". This replaces all error messages with just one.
    It is however adding another problem as I wanted to have 2 messages - one if email is empty "Please input your email" and one if email is incorrect "Your email address is incorrect".

    [PHP]
    $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);
    [/PHP]

  9. #9
    Stryks is offline Member
    Join Date
    Mar 2009
    Posts
    78

    Default

    You can get what you want by setting individual error messages by failure condition. You might end up actually specifying more than two error types, but if you set them all to have one of two messages, then from the users perspective, there will only be two.

    Take a look at THIS THREAD for more info.

    Yell out if you need more specific help.

    Cheers

  10. #10
    panosru is offline Junior Member
    Join Date
    Aug 2008
    Posts
    6

    Default

    Indeed there is not any way to set the number of the messages per validator and the only way is to extend the validator's class, personally instead of setting the message in getMessages() method I prefer to do something like the following in order to get only the first validation error.

    Code:
    <?php
    class My_Validator_EmailCheck extends Zend_Validate_EmailAddress {
    	public function getMessages() {
    		$messages = array_values($this->_messages);
    		return (array)$messages[0]; //Return only the first error for each time
    	}
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Single Error Message For Single Validator
    By gregl83 in forum General Q&A on Zend Framework
    Replies: 8
    Last Post: 08-01-2010, 08:39 AM
  2. Change error message for setRequired() form validator
    By topcatxx in forum Core Infrastructure
    Replies: 3
    Last Post: 05-13-2010, 08:36 AM
  3. Zend_Form Custom Validator message problem
    By Eric_The_Red in forum Core Infrastructure
    Replies: 1
    Last Post: 04-03-2009, 10:08 PM
  4. A Question about EmailAddress validator
    By krego in forum Core Infrastructure
    Replies: 0
    Last Post: 11-06-2008, 06:50 AM
  5. Form Validator message
    By mamyte03 in forum Core Infrastructure
    Replies: 3
    Last Post: 03-28-2008, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •