How avoid that unique validator rule check updating model attribute?

Hi,

I have a problem with the unique validator rule.

For example I have a user who wants to update its email address, but he/she does not want to update its username. These two fields are uniques. So my problem is, when the user change its email address the uniqe validator works fine, but while user does not update its username field, the unique validator rule will check its username too in the database and will return with error. But I want to avoid that the unique validator role check the updating model’s field.

So when I updating the users attributes the unique validator rule will get errors, casuse it checks the current updating model’s field too…




/**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            ['username', 'filter', 'filter' => 'trim'],

            ['username', 'required'],

            ['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This username has already been taken.'],

            ['username', 'string', 'min' => 2, 'max' => 255],


            ['email', 'filter', 'filter' => 'trim'],

            ['email', 'required'],

            ['email', 'email'],

            ['email', 'string', 'max' => 255],

            ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],


            ['password', 'string', 'min' => 6],

            ['passwordRepeat', 'compare', 'compareAttribute' => 'password', 'message' => 'The password does not match.'],

        ];

    }




Yii just have that here is a link look at the scenarios

http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios

I do not think this is the solution. Because the email has to be unique. I cannot choose the same email address that one has already got it. But the unique does not skip my original email address this is my problem. And I could not write custom scenario…

Of course the scenario will avoid the email validation. But it will not valid if my email is same as other user’s email…

Um, it’s strange. Did you really get any problem? Are you sure that you are not worrying about an imaginary issue?

I believe that UniqueValidator will not return error if the target attribute has not been changed when you are updating the model.

Take a look at the “modelExists” method of UniqueValidator.

Take a look at the "if - else" block in the function.

As Alirz23 said above,




/**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            ['username', 'filter', 'filter' => 'trim'],

            ['username', 'required'],

            ['username', 'unique', 'targetClass' => '\app\models\User', 

                'on'=>'insert', 'message' => 'This username has already been taken.'],

            ['username', 'string', 'min' => 2, 'max' => 255],


            ['email', 'filter', 'filter' => 'trim'],

            ['email', 'required'],

            ['email', 'email'],

            ['email', 'string', 'max' => 255],

            ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],


            ['password', 'string', 'min' => 6],

            ['passwordRepeat', 'compare', 'compareAttribute' => 'password', 'message' => 'The password does not match.'],

        ];

    }




Here the validation of username happens only on insert scenario while email will be validated always.

I don’t think we need to have separate scenarios for “update” and “insert” when we use the unique validator. The validator itself will take care of the difference. (Please read the source code that I linked.)

Avoiding unique validator for "update" may result in a duplicated entry when the user changes the target attribute.

Greetings,

Got server error as 500 Internal Server Error , Response -

PHP Notice ‘yii\base\ErrorException’ with message ‘Undefined index: User’

I am getting this error when I used Unique validator in yii2 model.

Code

[[‘username’], ‘unique’],

if I removed this rule then no error. If someone can help me . it will be very useful for me to learn.

Thanx Sally.

Do you have


use common/models/User;

or

use app/models/User;

at the top of your code?