Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | 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:

Comments

  • ABOUT

    Having fifteen years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • SUBSCRIBE TO OUR FEED