在表单验证中使用 Zend_Validate_Identical

发布于 2024-11-07 13:18:30 字数 1172 浏览 1 评论 0原文

我正在尝试使用“相同”验证器来验证注册表中的两个密码是否相同,但它一直尝试根据我为令牌输入的实际单词而不是我想要验证的表单元素进行验证。代码如下所示:(这是我的表单模型构造函数..)

    $password = new Zend_Form_Element_Password('password');
    $password->addValidator('Regex',false,array('pattern' => '/^.*(?=.{6,20})(?=.*[\d])(?=.*[a-zA-Z])/'))
             ->addValidator('StringLength',false,array('max'=>20))
             ->setRequired(true);

    $password2 = new Zend_Form_Element_Password('password2');
    $password2->setRequired(true);
    $password2->addValidator('Identical',false,array('token'=>'password'));       
    $register = new Zend_Form_Element_Submit('register');

    $this->setDecorators(array(
                                array('ViewScript', 
                                       array('viewScript' => '_form_registration.phtml'))  
                              )     
                        );

    $this->addElements(array($firstName,$lastName,$email,$city,$password,$password2,$register));

它不是针对名为“password”的表单元素进行验证,而是不断尝试与实际的字符串“password”进行匹配。

我的解决方法是在之后创建一个验证器数据已发布到控制器,并根据发布数据进行验证,但如果有任何更模块化的方法来执行此操作(也称为将逻辑保留在表单构造函数中),我很想知道。

先感谢您

I'm trying to use the 'Identical' validator to validate whether two passwords are the same in my registration form, but it keeps trying to validate against the actual word I enter for the token rather than the form element that I want to validate against. Code looks like this: (This is my form model constructor..)

    $password = new Zend_Form_Element_Password('password');
    $password->addValidator('Regex',false,array('pattern' => '/^.*(?=.{6,20})(?=.*[\d])(?=.*[a-zA-Z])/'))
             ->addValidator('StringLength',false,array('max'=>20))
             ->setRequired(true);

    $password2 = new Zend_Form_Element_Password('password2');
    $password2->setRequired(true);
    $password2->addValidator('Identical',false,array('token'=>'password'));       
    $register = new Zend_Form_Element_Submit('register');

    $this->setDecorators(array(
                                array('ViewScript', 
                                       array('viewScript' => '_form_registration.phtml'))  
                              )     
                        );

    $this->addElements(array($firstName,$lastName,$email,$city,$password,$password2,$register));

Instead of validating against the form element called 'password' it keeps trying to match against the actual string 'password'

The work around I have is that I create a validator after the data has been posted to the controller, and validate against the post data, but if there is any more modular way to do this (AKA leaving the logic within the form constructor) I would love to know.

Thank you in advance

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

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

发布评论

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

评论(3

心病无药医 2024-11-14 13:18:30

您的表单输出正确吗?

我看到您使用的装饰器是 ViewScript,所以我猜测您正在使用其他脚本自己编写表单的 html。

如果是这样,您是否遵循 Zend 方式为元素分配名称和 id 值?如果不是,当您将值传递到表单时,上下文可能无法正确设置,并且找不到您需要检查的“密码”元素。

我现在的建议是使用表单默认装饰器输出表单,并查看 id 和名称如何查找元素。然后,尝试复制您正在使用的 form.phtml 中的这些名称。

Are you outputting your form correctly?

I see that the decorator you're using is ViewScript so I'm guessing that you are coding the form's html yourself in some other script.

If so, are you following the Zend way of assigning names and id values to your elements? If you aren't, when you pass in the values to your form the context might not be set up correctly and it won't find the 'password' element that you need to check against.

My suggestion right now is to ouput the form using the form default decorators and look at how the ids and names look for the elements. Then, try to copy those names in the form.phtml that you're using.

梦幻的心爱 2024-11-14 13:18:30

在“password2”元素上添加相同的验证器后。

尝试将 isValid() 函数重载到表单对象中,如下所示:



    public function isValid ($data)
    {
        $this->getElement('password2')    
             ->getValidator('Identical')
             ->setToken($data['password'])
             ->setMessage('Passwords don\'t match.');
        return parent::isValid($data);
    }

After add the Identical Validator on your 'password2' element.

Try to overload isValid() function into your Form Object like this:



    public function isValid ($data)
    {
        $this->getElement('password2')    
             ->getValidator('Identical')
             ->setToken($data['password'])
             ->setMessage('Passwords don\'t match.');
        return parent::isValid($data);
    }

清风夜微凉 2024-11-14 13:18:30

我一直遇到完全相同的问题。
通过使用外部函数重写代码以验证相同性来修复此问题。

<?php
class RegisterForm extends Zend_Form
{
    /**
     * create your form
     */
    public function init()
    {
        $this->addElements(array(
            new Zend_Form_Element_Password('password',
                array( 'label' => 'Password:',
                           'required' => true,
                           'filters' => array('StringTrim', 'StripTags'),
                           'validators' => array(array(StringLength', false, array(5, 25)))
                )
            ),
            new Zend_Form_Element_Password('pass_twice',
                array('label' => 'Pass Twice',
                         'required' => true,
                         'filters' => array('StringTrim', 'StripTags'),
                         'validators' => array('Identical')
                )
            )
        );
    }

    public function isValid($data)
    {
        $passTwice = $this->getElement('pass_twice');
        $passTwice->getValidator('Identical')->setToken($data['password']);
        return parent::isValid($data);
    }
}
?>

解决方案来自:http://emanaton.com/node/38

I have been having the exact same issue.
It was fixed by rewriting the code with an outside function to validate identical as such.

<?php
class RegisterForm extends Zend_Form
{
    /**
     * create your form
     */
    public function init()
    {
        $this->addElements(array(
            new Zend_Form_Element_Password('password',
                array( 'label' => 'Password:',
                           'required' => true,
                           'filters' => array('StringTrim', 'StripTags'),
                           'validators' => array(array(StringLength', false, array(5, 25)))
                )
            ),
            new Zend_Form_Element_Password('pass_twice',
                array('label' => 'Pass Twice',
                         'required' => true,
                         'filters' => array('StringTrim', 'StripTags'),
                         'validators' => array('Identical')
                )
            )
        );
    }

    public function isValid($data)
    {
        $passTwice = $this->getElement('pass_twice');
        $passTwice->getValidator('Identical')->setToken($data['password']);
        return parent::isValid($data);
    }
}
?>

Solution from: http://emanaton.com/node/38

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