zend form email validation

27,073

Solution 1

Well, it's a late answer but I think is always useful.

Simply add true as second param of addValidator()

From Zend docs (http://framework.zend.com/apidoc/1.8/):

addValidator (line 67)

Adds a validator to the end of the chain

If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain, if one exists, will not be executed.

return: Provides a fluent interface

access: public

Here the signature:

Zend_Validate addValidator (Zend_Validate_Interface $validator, [boolean $breakChainOnFailure = false])

Zend_Validate_Interface $validator
boolean $breakChainOnFailure

So the code is:

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
    ->addFilters(array('StringTrim', 'StripTags'))
    ->addValidator('EmailAddress',  TRUE  ) // added true here
    ->addValidator(new Zend_Validate_Db_NoRecordExists(
                array(
                        'adapter'=>Zend_Registry::get('user_db'),
                        'field'=>'email',
                        'table'=>'tbl_user'
                        ), TRUE )
            );

Solution 2

You have to create an instance of the Zend_Validate_EmailAddress class and call the setMessages method and then override the messages that you like, to remove the ones that you mention it would be something like this:

$emailValidator->setMessages(array(
    Zend_Validate_EmailAddress::INVALID_FORMAT => "Your error message",
    Zend_Validate_Hostname::INVALID_HOSTNAME => "Your error message",
    Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED => "Your error message"
));

I hope this help somebody :-)

Solution 3

$email->addErrorMessage("Please Enter Valid Email Address");

you can use custom validator. create a file Email.php inside folder Validate in your library folder at the root of project

class Validate_Email extends Zend_Validate_Abstract
{

    const INVALID = 'Email is required';
    protected $_messageTemplates = array(
    self::INVALID => "Invalid Email Address",
    self::ALREADYUSED => "Email is already registered"
    );

     public function isValid($value)
     {
          if(preg_match($email_regex, trim($value))){
            $dataModel = new Application_Model_Data(); //check if the email exists
            if(!$dataModel->email_exists($value)){
                return true;
            }
            else{
                $this->_error(self::ALREADYUSED);
                return false;
            }
          }
          else
          {
            $this->_error(self::INVALID);
            return false;
          }
     }
}

and in you form.php file

    $mailValidator = new Validate_Email();
    $email->addValidator($mailValidator, true);

Don't know if it works or not but for me it worked in case of telephone. Courtesy of http://softwareobjects.net/technology/other/zend-framework-1-10-7-telephone-validator/

Share:
27,073
Bryan
Author by

Bryan

Updated on October 07, 2020

Comments

  • Bryan
    Bryan over 3 years

    I have the following code to generate an input field for user's email address

    $email = new Zend_Form_Element_Text('email');
    $email->setLabel('Email:')
        ->addFilters(array('StringTrim', 'StripTags'))
        ->addValidator('EmailAddress')
        ->addValidator(new Zend_Validate_Db_NoRecordExists(
                                                            array(
                                                                    'adapter'=>Zend_Registry::get('user_db'),
                                                                    'field'=>'email',
                                                                    'table'=>'tbl_user'
                                                                    )))
        ->setRequired(true)
        ->setDecorators(array(
                                array('Label', array('escape'=>false, 'placement'=>'append')),
                                array('ViewHelper'),
                                array('Errors'),
                                array('Description',array('escape'=>false,'tag'=>'div')),
                                array('HtmlTag', array('tag' => 'div')),
                            ));
    $this->addElement($email);
    

    now the problem is if user enter invalid hostname for email, it generate 3 errors. lets say user enter 'admin@l' as email address, and the errors will be
    * 'l' is no valid hostname for email address 'admin@l'
    * 'l' does not match the expected structure for a DNS hostname
    * 'l' appears to be a local network name but local network names are not allowed

    I just want it to give only one custom error instead of all these. If I set error message "Invalid Email Address" by addErrorMessage method, it will again generate the same message against the db_validation.

  • Bryan
    Bryan over 13 years
    yes, but I also have DB validator, which checks if the email is already used to register with the site. and if I add the above message, even in the case when the email is valid but exists in database, it will display the custom message.
  • Santosh Linkha
    Santosh Linkha over 13 years
    Check it out !! Worked then better if not worked then it's okay!!