Foreign Worker
Foreign Worker

Reputation: 67

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

Upvotes: 3

Views: 1090

Answers (2)

Midhun
Midhun

Reputation: 3988

try using the rule like this

 // validates if the value of "password" attribute equals to that of 
 ['password', 'compare', 'message'=>"Passwords don't match"],

it automatically compares the password value to attribute password_repeat instead of doing it in the other order as explained in the documentation .

http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#compare

Upvotes: 2

ScaisEdge
ScaisEdge

Reputation: 133360

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']) ?>

Upvotes: 1

Related Questions