在表单验证中使用 Zend_Validate_Identical
我正在尝试使用“相同”验证器来验证注册表中的两个密码是否相同,但它一直尝试根据我为令牌输入的实际单词而不是我想要验证的表单元素进行验证。代码如下所示:(这是我的表单模型构造函数..)
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的表单输出正确吗?
我看到您使用的装饰器是 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.
在“password2”元素上添加相同的验证器后。
尝试将 isValid() 函数重载到表单对象中,如下所示:
After add the Identical Validator on your 'password2' element.
Try to overload isValid() function into your Form Object like this:
我一直遇到完全相同的问题。
通过使用外部函数重写代码以验证相同性来修复此问题。
解决方案来自: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.
Solution from: http://emanaton.com/node/38