validateAttribute() public method

public validateAttribute ( $model, $attribute )
示例#1
0
 public function uniqueCheck()
 {
     if ($this->owner instanceof ActiveRecordInterface) {
         /** @var Model $model */
         $model = clone $this->owner;
         $uniqueValidator = new UniqueValidator();
         $uniqueValidator->validateAttribute($model, $this->slugAttributeName);
         return !$model->hasErrors($this->slugAttributeName);
     }
     throw new Exception('Can\'t check if the slug is unique.');
 }
示例#2
0
 /**
  * @inheritdoc
  */
 public function validateAttribute($model, $attribute)
 {
     if (is_array($this->targetAttribute) && count($this->targetAttribute) > 1) {
         $skip = false;
         $keyAttribute = $attribute;
         foreach ($this->targetAttribute as $key => $value) {
             $keyAttribute = is_int($key) ? $value : $key;
             $skip = $this->skipOnError && $model->hasErrors($keyAttribute) || $this->skipOnEmpty && $this->isEmpty($model->{$keyAttribute});
             if ($skip) {
                 break;
             }
         }
         if (!$skip && $keyAttribute == $attribute) {
             parent::validateAttribute($model, $attribute);
         }
     } else {
         parent::validateAttribute($model, $attribute);
     }
 }
示例#3
0
 public function testValidateCompositeKeys()
 {
     $val = new UniqueValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['order_id', 'item_id']]);
     // validate old record
     $m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     $m->item_id = 1;
     $val->validateAttribute($m, 'order_id');
     $this->assertTrue($m->hasErrors('order_id'));
     // validate new record
     $m = new OrderItem(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertTrue($m->hasErrors('order_id'));
     $m = new OrderItem(['order_id' => 10, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     $val = new UniqueValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['id' => 'order_id']]);
     // validate old record
     $m = Order::findOne(1);
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = Order::findOne(1);
     $m->id = 2;
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = Order::findOne(1);
     $m->id = 10;
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
     $m = new Order(['id' => 1]);
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = new Order(['id' => 10]);
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
 }