3

I have a problem with identical validator of zend framework. I have two elements (password and verify password) and wanna make sure they are identical. But identical validator does not work for me. The tokens are always not match:

class Form_MemberRegisterationForm extends Zend_Form
{
    public function init()
    {                
            $password = $this->createElement('password', 'password1');
        $password->setLabel('Password:');
        $password->setRequired(TRUE);
        $password->setAttrib('size', 30);
        $password->setErrorMessages(array ("isEmpty" => "Invalid Password!" ));
        $this->addElement($password);
        //      
        $confirmPswd = $this->createElement('password', 
            'confirmPassword');
        $confirmPswd->setLabel('Verify password:');
        $confirmPswd->setAttrib('size', 30);
        $confirmPswd->addValidator('identical', false, 
            array ('token' => 'password1' ));

        $this->addElement($confirmPswd);

What I am doing wrong?

3 Answers 3

4

Your code is correct if your Zend Framework version is over 1.10.5.

For earler version, try to add validator in overrided isValid method:

public function isValid($data)
{
    $this->getElement('passwordConfirm')->addValidator('identical', false, 
        array('token' => $data['password'])
    );
    return parent::isValid($data);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Error message: No token was provided to match against
As of ZF 1.10.5 when a token is set in the Identical validator as part of a form it will check field names and is not just a value reference.
Thank you, tomjowitt. I corrected my answer and enable wiki for it.
1

The code example is correct but it will only work if your Zend Framework version is over 1.10.5 which is when the feature was introduced that allows you to refer to other form elements with the token parameter.

I'm guessing your ZF version is under 1.10.5?

Using a more up-to-date version of ZF will mean you don't have to worry about overriding isValid methods and will help make your code easier to understand.

Explanation from one of the ZF devs here:

http://zfuniversity.com/tag/zend_validate_identical/

Comments

0

try this way

//password
$this->addElement('password', 'password', array('label' => 'Password', 'required' => true));

//password_confirm
$this->addElement('password', 'password_confirm', array('label' => 'Password Confirm', 'required' => true));
$this->password_confirm->addValidator('Identical', false, array('token' => 'password'));

P.S. controls to perform well in the form isValid controller, because otherwise you'll never displayed error messages! ;)

1 Comment

It says: Tokens do not match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.