Zend 框架相同验证器不起作用

发布于 2024-11-27 01:47:53 字数 916 浏览 2 评论 0原文

我对 zend 框架的相同验证器有问题。我有两个元素(密码和验证密码)并想确保它们相同。但相同的验证器对我不起作用。令牌总是不匹配:

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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-12-04 01:47:53

如果您的 Zend Framework 版本高于 1.10.5,您的代码是正确的

对于早期版本,尝试在重写的 isValid 方法中添加验证器:

public function isValid($data)
{
    $this->getElement('passwordConfirm')->addValidator('identical', false, 
        array('token' => $data['password'])
    );
    return parent::isValid($data);
}

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);
}
悲欢浪云 2024-12-04 01:47:53

代码示例是正确的,但仅当您的 Zend Framework 版本超过 1.10.5 时才有效,此时引入了允许您使用 token 参数引用其他表单元素的功能。

我猜你的 ZF 版本低于 1.10.5?

使用更新版本的 ZF 将意味着您不必担心重写 isValid 方法,并且有助于使您的代码更易于理解。

来自 ZF 开发人员之一的解释:

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

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/

深居我梦 2024-12-04 01:47:53

试试这个

//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'));

方法控件在 isValid 控制器中表现良好,否则您将永远不会显示错误消息! ;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文