Zend 框架相同验证器不起作用
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的 Zend Framework 版本高于 1.10.5,您的代码是正确的。
对于早期版本,尝试在重写的 isValid 方法中添加验证器:
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:
代码示例是正确的,但仅当您的 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/
试试这个
方法控件在 isValid 控制器中表现良好,否则您将永远不会显示错误消息! ;)
try this way
P.S. controls to perform well in the form isValid controller, because otherwise you'll never displayed error messages! ;)