Custom Zend Validate EmailAddress Error Message
Posted on | April 23, 2012 | 1 Comment
Anyone who has tried to validate a user input using Zend_Validate_EmailAddress will find that there are a LOT of error messages returned – one for each problem with the email address:
- Invalid format
- Invalid hostname
- Invalid mx record
- and 5 more
This class will replaces all the error messages with one message.
Solution
<?php /** * Override Zend_Validate_EmailAddress so that only one error message shows when there is a problem */ class RPK_Validate_EmailAddress extends Zend_Validate_EmailAddress { /** * Returns true if and only if $value is a valid email address * according to RFC2822 * @param string $value * @return boolean */ public function isValid($value) { $response = parent::isValid($value); if (!$response) { $this->_messages = array(self::INVALID => "Please enter a valid email address"); } return $response; } }
Useage
<?php //in form object $element->addValidator(new RPK_Validate_EmailAddress());
Posted By:Richard Parnaby-King