Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Compare Validator alert not disappear

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 compare validation

like image 585
Dedy Kurniawan Avatar asked Oct 14 '14 05:10

Dedy Kurniawan


People also ask

How to validate each item in an array in yii2?

Since Yii2 version 2.0.4 there is the EachValidator used to validate each item in an array. The ['integer'] part can be every other validator object that Yii2 offers and can hold the specific arguments for the validator. Like: ['integer', 'min' => 1337]. If the userIDs doesn't contain an array the rule validation will fail.

How do I use the compare validator to compare dates?

The compare validator can only be used to compare strings and numbers. If you need to compare values like dates you have two options. For comparing a date against a fixed value, you can simply use the date validator and specify its $min or $max property.

Why do I need to override the eachvalidator when using the form?

Changing framework files is a very bad thing to do so we need to override it when using the form: Since Yii2 version 2.0.4 there is the EachValidator used to validate each item in an array. The ['integer'] part can be every other validator object that Yii2 offers and can hold the specific arguments for the validator.

What happens if email validation fails?

If false, it will use message as the error message. Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message. This validator checks if the input value is a valid email address.


2 Answers

In my case I just changed password validation from this:

['password','compare'],

to this:

['password_repeat', 'compare', 'compareAttribute' => 'password'],
like image 56
Victor Avatar answered Dec 06 '22 02:12

Victor


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):

<?php $form = ActiveForm::begin(['enableClientValidation' => false']);?>
like image 43
Claudio Eddy Avatar answered Dec 06 '22 02:12

Claudio Eddy