开发者

Zend framework identical validator does not work

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?


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);
}


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/


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! ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜