0

我正在使用 Yii 的内置比较验证器来比较两个表单属性:

<div class="row ">
<?php echo $form->labelEx($model, 'newPassword', array('class'=>'control-label'));?>
<?php echo $form->passwordField($model, 'newPassword',array('class'=>'span5','maxlength'=>100)); ?>
<?php echo $form->error($model, 'newPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'confirmPassword', array('class'=>'control-label'));?>
<?php echo $form->passwordField($model, 'confirmPassword',array('class'=>'span5','maxlength'=>100)); ?>
<?php echo $form->error($model, 'confirmPassword'); ?>
</div>

与规则:

array('newPassword', 'length', 'max'=>100,'min'=>6),
array('newPassword', 'compare', 'compareAttribute'=>'username','operator'=>'!=', 'message'=>'Password must not be the same as username'),
array('newPassword', 'compare', 'compareAttribute'=>'confirmPassword','message'=>'Please enter the same password twice'),
array('confirmPassword', 'safe'),

当我第一次输入 newPassword 时,会触发验证并显示“输入相同”的错误消息。如果我然后正确输入 confirmPassword,它不会隐藏。如果我然后更改 confirmPassword,然后更改 newPassword 以匹配,则错误隐藏。它似乎只在我更改 newPassword 字段时才起作用 - 所以我必须向后填写表格才能正确隐藏错误。

我错过了什么吗?

[编辑] 我可以强制验证在具有 compareAttribute 规则的字段上触发,方法是使用 JavaScript 暂时清空字段的值,触发验证,然后重新应用以前的值并再次触发验证。不过似乎有点疯了。

4

2 回答 2

2

我猜你正在使用客户端验证?

我相信这不能按您的意愿工作的原因是当您更改除 newPassword 字段之外的字段时,不会触发 newPassword 的规则。

不幸的是,我不确定当您更改另一个字段时如何在一个字段上触发验证,尽管看起来您之前曾问过这个问题。Jon 的回答似乎建议您可以$.fn.yiiactiveform.updateInput在 javascript 中使用 onchange 来触发验证?

于 2012-08-13T16:48:51.297 回答
-2

设置运算符

array('newPassword', 'compare', 'compareAttribute'=>'confirmPassword','operator'=>'==' ,'message'=>'Please enter the same password twice'),**strong text**
于 2014-08-14T07:32:44.203 回答