我在 yii2 表单中使用 compareValidator 。
这是代码
[['passwordConfirm'], 'compare', 'compareAttribute' => 'password'],
它应该工作正常,因为它的文档中的代码也是
但是,它只会验证密码一次。
例如,如果我输入密码和确认密码,一旦验证正常,但如果我返回密码字段并更改密码比没有将确认密码输入与新的密码输入进行比较
其他人有同样的问题??? 或者我在这里做错了什么
以下是model
代码
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
[['passwordConfirm'], 'compare', 'compareAttribute' => 'password'],
];
}
public function attributeLabels()
{
return [
'passwordConfirm' => 'Confirm Password',
'username' => 'Username',
'email' => 'Email',
];
}
这是view
代码
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'username')->textInput( ['data-toggle' => 'tooltip',
'data-placement' =>
'right',
'title' => 'Username should contain at least 6 characters'
]) ?>
<?= $form->field($model, 'password')->passwordInput(['data-toggle' => 'tooltip',
'data-placement' =>
'right',
'title' => 'Password should contain at least 6 characters',
]) ?>
<?= $form->field($model, 'passwordConfirm')->passwordInput() ?>
<?= $form->field($model, 'email') ?>
<?php ActiveForm::end(); ?>
谢谢