Yii2: compareValidator javascript validator only works with attributes in rules

Created on 3 Mar 2018  路  6Comments  路  Source: yiisoft/yii2

since commit link the javascript for compareValidator does not work anymore, because the targetAttribute is only found when it's in the rules section.

What steps will reproduce the problem?

public function rules()
    {
        return [
            ['password', 'compare'],
        ];
    }

What is the expected result?

password validates correctly to the password_repeat attribute.

What do you get instead?

It always does not validate and gives an error while both values are the same, because the password_repeat attribute was not found in the client validation method.

Suggestion

Adding password_repeat as safe attribute was also not working. Adding another validator linke StringValidator for password_repeat fixed the validation. Like:

public function rules()
    {
        return [
            ['password', 'compare'],
            [['password_repeat'], 'string', 'length' => [6, 40]],
        ];
    }

My suggestion is to add a warning or notice to the validator, that the target attribute must be an attribute in the rules section.

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.14.1
| PHP version | 7.2.3
| Operating system | Linux

to be verified bug

All 6 comments

@mikk150 could you check this issue, please?

@SilverFire Yes, this is true as there is no validator on password_repeat therefore this attribute does not exist in yiiActiveForm.attributes even tho actually it could exist

What I would do is register all fields that are rendered using ActiveForm::field($model, 'attribute') to yiiActiveForm.attributes just without validators

What I would do is register all fields that are rendered using ActiveForm::field($model, 'attribute') to yiiActiveForm.attributes just without validators

I'm not sure how safe is it (in terms of backward compatibility).

Maybe, we can fallback to ID search in case attribute was not find in attributes?

Also found this bug.

My rules are:

public function rules()
    {
        return [
            ['password', 'safe'],
            ['passwordRepeat', 'compare', 'compareAttribute' => 'password'],
            ...
        ];
    }

I suggest to fill yiiActiveForm.attributes with all safe attributes, (because they are actually the one, which should be validated and input is allowed)

And this is pretty critical for already live projects, which started using new json field support and they can't revert, so would be great to fix this in neartime.

@aprokopenko this issue is not in priority for the nearest future, so if you have time, would you like to help up to fix this issue by submitting a pull request? We have contributors guide that is a good starting point. If you need any tip 鈥撀爈et us know.

Was this page helpful?
0 / 5 - 0 ratings