Yii2: unique on update is giving error with target class.

Created on 31 Mar 2016  路  9Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

i have two tables user and profile,user related mobile i am saving to profile.I am saving data using user model nd define mobile variable,everything is fine, i am checking unique on mobile using target class it is giving error if it is unique,now problem is when i update it is giving me same error.

What is the expected result?

on update it should not give error if attribute is same as old attribute.

What do you get instead?

i am getting unique error on update.

Additional info

i have email attribute email in user model ,it is also checking unique and it is fine even on update.but mobile is coming from profile class so it is giving error.
i follow this link but still getting error.
http://blog.tural.us/saving-and-validating-only-changed-dirty-values-on-yii2/

| Q | A |
| --- | --- |
| Yii version | 2.0.7 |
| PHP version | 5.6.11 |
| Operating system | ubuntu 15.10 |

question need more info

Most helpful comment

@subhtak sorry, it seems your question was overseen.

This behavior is expected because your mobile already exists in Profile table. Use when condition to run this validation rule only when its value changed:

public function rules()
{
   return [
       ['mobile', 'unique',
           'targetClass' => 'app\models\Profile', 
           'message' => \Yii::t('app', 'This Mobile number has already been taken'), 
           'when' => function ($model, $attribute) {
               return $model->{$attribute} !== $model->getOldAttribute($attribute);
           },
      ],
   ];
}

All 9 comments

How to reproduce your error?

two table user nd profile.
user related data i m saving into profile
i have form where i m creating user there i have field mobile that i m saving in profile.
so in rules i have unique for mobile and target class is profile.
on create it gives me error if profile table has same mobile no but on update it is giving me same error.so on update it is checking it is unique or not.

my question is how to implement unique rule on mobile with related class
below is my code but not workin on update.

class User extends BaseModel 
{
    public $mobile;

 /**
     * @inheritdoc
     */
    public function rules()
    {
       return [
                   ['mobile', 'unique','targetClass' => 'app\models\Profile', 
              'message' => \Yii::t('app', 'This Mobile number has already been taken')],
       ];
    }

any solution for this please?

@subhtak sorry, it seems your question was overseen.

This behavior is expected because your mobile already exists in Profile table. Use when condition to run this validation rule only when its value changed:

public function rules()
{
   return [
       ['mobile', 'unique',
           'targetClass' => 'app\models\Profile', 
           'message' => \Yii::t('app', 'This Mobile number has already been taken'), 
           'when' => function ($model, $attribute) {
               return $model->{$attribute} !== $model->getOldAttribute($attribute);
           },
      ],
   ];
}

_This is an automated comment, triggered by adding the label question._

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

I am sorry but this is wrong.why it is closed.I already tried your solution ,I used when it is not working.please give me solution that is working.

@ajkosh You're better off writing a custom validator for this procedure.

unique validator not correct work out active record

uniq validator ONLY for AR in others cases it work as not exist validator

look it https://github.com/yiisoft/yii2/pull/11353 and can try - it pass all test and save BC (if you not use targetClass in AR of current AR)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sapsxxxil picture sapsxxxil  路  50Comments

spiritdead picture spiritdead  路  67Comments

cebe picture cebe  路  53Comments

Mirocow picture Mirocow  路  56Comments

alexandernst picture alexandernst  路  163Comments