Yii2比较验证器警报不会消失

时间:2021-05-17 22:51:43

I type a password and then I repeat it on repeat password field but red alert didn't disappear, and when I click submit button it was success and no error validation. How to make compare alert disappear when I repeated password?

我键入一个密码然后我在重复密码字段重复它,但红色警报没有消失,当我点击提交按钮它是成功的,没有错误验证。重复密码时如何使比较警报消失?

Here's my rules code in 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', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

        ['password', 'required'],
        ['password','compare'],
        ['password', 'string', 'min' => 6],
        ['password_repeat','safe']

    ];
}

and my form

和我的形式

<?php $form = ActiveForm::begin(); ?>

<h3>Your Account</h3>
<?= $form->field($modelUser, 'username')->textInput(['maxlength' => 45, 'class' => 'input-xlarge form-control']) ?>

<?= $form->field($modelUser, 'password')->passwordInput(['class' => 'form-control input-xlarge']) ?>

<?= $form->field($modelUser, 'password_repeat')->passwordInput(['class' => 'form-control input-xlarge']) ?>
<button class="btn btn-primary" type="submit">Continue</button>

<?php ActiveForm::end(); ?>

and here's my screenshot Yii2比较验证器警报不会消失

这是我的截图

3 个解决方案

#1


16  

In my case I just changed password validation from this:

在我的情况下,我刚刚更改了密码验证:

['password','compare'],

to this:

对此:

['password_repeat', 'compare', 'compareAttribute' => 'password'],

#2


2  

If I understood you correctly: you type the first password and, as you change focus to the other field (password_repeat), the form already shows an error message even though you haven't even typed the second field. If it's that, you could disable client validation, so that the data would be validated only after you submit the form. To do so, you could add the following to your ActiveForm initialization (it's an option):

如果我理解正确:您键入第一个密码,并且当您将焦点更改为其他字段(password_repeat)时,即使您甚至没有键入第二个字段,表单也会显示错误消息。如果是这样,您可以禁用客户端验证,这样只有在您提交表单后才能验证数据。为此,您可以将以下内容添加到ActiveForm初始化(这是一个选项):

<?php $form = ActiveForm::begin(['enableClientValidation' => false']);?>

#3


0  

I just only needed this line:

我只需要这一行:

[['password_repeat'], 'compare', 'compareAttribute' => 'password', 'message' => 'Your error message']

#1


16  

In my case I just changed password validation from this:

在我的情况下,我刚刚更改了密码验证:

['password','compare'],

to this:

对此:

['password_repeat', 'compare', 'compareAttribute' => 'password'],

#2


2  

If I understood you correctly: you type the first password and, as you change focus to the other field (password_repeat), the form already shows an error message even though you haven't even typed the second field. If it's that, you could disable client validation, so that the data would be validated only after you submit the form. To do so, you could add the following to your ActiveForm initialization (it's an option):

如果我理解正确:您键入第一个密码,并且当您将焦点更改为其他字段(password_repeat)时,即使您甚至没有键入第二个字段,表单也会显示错误消息。如果是这样,您可以禁用客户端验证,这样只有在您提交表单后才能验证数据。为此,您可以将以下内容添加到ActiveForm初始化(这是一个选项):

<?php $form = ActiveForm::begin(['enableClientValidation' => false']);?>

#3


0  

I just only needed this line:

我只需要这一行:

[['password_repeat'], 'compare', 'compareAttribute' => 'password', 'message' => 'Your error message']