Pushpendra
Pushpendra

Reputation: 4392

Strange issue with Zend_Validate_Identical?

I had write the following code in my Zend Form:

    $newpassword = new Zend_Form_Element_Password('newpassword');
    $newpassword->setLabel("Enter your New Password :")
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator(
                     'NotEmpty',
                      TRUE,
                      array('messages' => array(
                              'isEmpty' => 'Please enter new password.'
                                               )
                           )
                     )
               ->addValidator(
                    'Alnum',
                     TRUE,
                     array('messages' => array(
                                 'alnumInvalid' => 'Please enter a valid new password.',
                                 'notAlnum' => 'Please enter a valid new password.',
                                 'alnumStringEmpty' => 'Please enter a valid new password.'
                                              )
                          )
                          )
               ->addValidator('StringLength', false, array(4, 25))
               ->getValidator('StringLength')->setMessage("Should be 4-25 characters long.");


    $retypepassword = new Zend_Form_Element_Password('retypepassword');
    $retypepassword->setLabel("Retype-Password :")
                   ->setRequired(true)
                   ->addFilter('StripTags')
                   ->addFilter('StringTrim')
                   ->addValidator(
                        'NotEmpty',
                        TRUE,
                        array('messages' => array(
                                         'isEmpty' => 'Please enter confirm password.'
                                                 )
                             )
                       )
                    ->addValidator(
                         new Zend_Validate_Identical('newpassword'),
                         TRUE,
                         array('messages' => array(
 'notSame' => 'New password and confirm password are not matching. They must be same.',
 'missingToken' => 'New password and confirm password are not matching. They must be same.'
                                                  )
                                           )
                                  )
              ->addValidator('StringLength', false, array(4, 25))
              ->getValidator('StringLength')->setMessage("Should be 4-25 characters long.");

As you can see that I had override the error messages for Zend_Validate_Identical i.e. for notSame and missignToken, but still the form is showing the default error message i.e. "The two given tokens do not match"

Can anyone please help me.

Thanks In Advance...

Upvotes: 1

Views: 1477

Answers (1)

John Flatness
John Flatness

Reputation: 33789

The $options parameter to Zend_Form_Element::addValidator gets ignored completely if you pass it a validator object (which you're doing by passing new Zend_Validate_Identical).

Instead, you want to do something closer to the way you've added your other validators:

$this->addValidator(
    'Identical',
    TRUE,
    array('token' => 'newpassword',
          'messages' => array(
              'notSame' => 'New password and confirm password are not matching. They must be same.',
              'missingToken' => 'New password and confirm password are not matching. They must be same.'
          )
    )
);

Upvotes: 3

Related Questions