Search code examples
phpvalidationyiiyii2client-side-validation

Compare rule yii2 doesn't work correctly


I have my rules method like this:

public function rules()
{
    return [
        [['username', 'email', 'password'],'filter', 'filter' => 'trim'],
        [['username', 'email', 'password'],'required', 'message' => '{attribute} can not be empty'],
        ['username', 'string', 'min' => 2, 'max' => 255],
        ['password', 'string', 'min' => 6, 'max' => 255],
        ['password_repeat', 'required', 'message' => 'This field can not be empty'],
        ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match", 'skipOnError' => true],
        ['username', 'unique',
            'targetClass' => User::className(),
            'message' => 'This name is already used.'],
        ['email', 'email'],
        ['email', 'unique',
            'targetClass' => User::className(),
            'message' => 'This name is already used.'],
    ];
}

And my view code is like this:

<?php $form = ActiveForm::begin(['action' => 'login/register']); ?>
        <?= $form->field($registration, 'username',
            ['template' => '<div class="uk-form-row">
                                {input}{error}
                                </div>'])
            ->textInput(['id' => 'register_username', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'password',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput(['id' => 'register_password', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'password_repeat',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput(['id' => 'register_password_repeat', 'class' => 'md-input']) ?>

        <?= $form->field($registration, 'email',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->textInput(['id' => 'register_email', 'class' => 'md-input']) ?>
        <div class="uk-margin-medium-top">
            <button class="md-btn md-btn-primary md-btn-block md-btn-large">Sign in</button>
        </div>
        <?php ActiveForm::end(); ?>

When I'm filling all given fields I have an error Passwords don't match when repeat the password even when it's correct at first time. Is there something with my validation rules or is it a bug in Yii Validator?

UPD: I've tried 'skipOnError' => true. I found it as an answer for the similar question but it still doesn't work as it's expected.

UPD: I did some validation in my console:

var a = $('#register_password')
undefined
a.val()
"Halloha"
var b = $('#register_password_repeat')
undefined
b.val()
"Halloha"

But it still shows Passwords don't match error message


Solution

  • Try avoid the id in password input (the id is automatically generated by yii2)

     <?= $form->field($registration, 'password',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput(['class' => 'md-input']) ?>
    
    <?= $form->field($registration, 'password_repeat',
            ['template' => '<div class="uk-form-row">
                            {input}{error}
                            </div>'])
            ->passwordInput([ 'class' => 'md-input']) ?>