How To Skip A Validator Inside My Own One?

There is my own validator. I have a condition when i have to skip another yii validator

For example:




public function rules()

{

 return array(

     array('an_unique_field','validSomething','on'=>array('create','edit')),

     array('an_unique_field', 'unique' .... )

}


........


public function validSomething($attribute,$params)

{

if ($this->scenario == 'edit')

{

$model = Table::model()->findByPk($this->id,array('select'=>'an_unique_field'));


if ($model->an_unique_field == $this->$attribute)

//skip unique validator

}




}

Is this possible to skip another validator inside my own?

The unique validator already does what (I think) you’re attempting to do. It only adds an error if it finds another record with the same attribute value but a different primary key.

I try to change a record (for example i’m going to change a text field for description) but I will not change the unique field. But the field has the same value as in database. So I will get the error. But I actually was not trying to change the unique field, was I?

So if I change the unique field the validator must work otherwise I dont need to valid it as I have not made changes at all.

if Keith is right then you can achieve what you want by giving scenario like …




array('an_unique_field', 'unique','on'=>'insert')



it will not be called on edit(update).

Sometimes I DO need update if I have made changes.

use below custom function for unique check like




public function validSomething($attribute, $params) {

        

            $model = Table::model()->find("an_unique_field=".$this->an_unique_field);


            if (count($model)>0){

                $this->addError($attribute, 'unique error msg.');

            }

        

    }



and remove ‘on’=>array(‘create’,‘edit’)

I don’t understand. Are you saying you’re happy to have non-unique values for that attribute if the records already exist? Specifically, are you saying that non-unique values already exist in the database table?

There are 3 actions:

1: create a record. Have to check for unique.

2: edit a record and the unique field has been changed. Have to check for unique.

3: edit a record and has NOT been changed. Obviously dont’ have to check. No changes for the field.

1st and 2nd - everything is clear

But 3rd is my problem.

Why not just allow the validator to run anyway? It will work correctly and the overhead will be negligible unless you’re running a huge number of updates.