Bug in UniqueValidator


(Mestre Alessandros) #1

The standard behavior, when checking for a unique rule, is to check only when the target attributes don’t have any other validation errors, not only the source attributes.

Ex:

Att A is integer, text is typed, but it have a integer rule, an error is added.

Att B have an integer rule and have passe

Att (A , B ) ( or just B ) have a unique rule with target attribute (A,B ). A is not evaluated(since it has an error) , but B is. since A is a text the validation code runs and trows a DBException.

I am using a userfunction on When property to bypass this behavior , but is pretty logical that it should be the default behavior to skip this validation.

so i recommend this changes on the uniquevalidator.php , line 83

before




if (is_array($targetAttribute)) {

            $params = [];

            foreach ($targetAttribute as $k => $v) {

                $params[$v] = is_integer($k) ? $model->$v : $model->$k;

            }

        } else {

            $params = [$targetAttribute => $model->$attribute];

        }



after




if (is_array($targetAttribute)) {

            $params = [];

            foreach ($targetAttribute as $k => $v) {

                if ( $this->skipOnError && $model->hasErrors($v)

                || $this->skipOnEmpty && $this->isEmpty($model->$v))

                   return;

                $params[$v] = is_integer($k) ? $model->$v : $model->$k;

            }

        } else {

            if ( $this->skipOnError && $model->hasErrors($targetAttribute )

                || $this->skipOnEmpty && $this->isEmpty($model->$targetAttribute))

                return;

            $params = [$targetAttribute => $model->$attribute];

        }




(Bizley) #2

I recommend you to report all issues at https://github.com/yiisoft/yii2/issues so dev team can take care of it faster.