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
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.
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.
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.
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.
In my case I just changed password validation from this:
['password','compare'],
to this:
['password_repeat', 'compare', 'compareAttribute' => 'password'],
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']);?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With