Ejemplo n.º 1
0
 public function testValidateAttributeDefault()
 {
     $val = new UniqueValidator();
     $m = ValidatorTestMainModel::find()->one();
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
     $m = ValidatorTestRefModel::findOne(1);
     $val->validateAttribute($m, 'ref');
     $this->assertTrue($m->hasErrors('ref'));
     // new record:
     $m = new ValidatorTestRefModel();
     $m->ref = 5;
     $val->validateAttribute($m, 'ref');
     $this->assertTrue($m->hasErrors('ref'));
     $m = new ValidatorTestRefModel();
     $m->id = 7;
     $m->ref = 12121;
     $val->validateAttribute($m, 'ref');
     $this->assertFalse($m->hasErrors('ref'));
     $m->save(false);
     $val->validateAttribute($m, 'ref');
     $this->assertFalse($m->hasErrors('ref'));
     // array error
     $m = FakedValidationModel::createWithAttributes(['attr_arr' => ['a', 'b']]);
     $val->validateAttribute($m, 'attr_arr');
     $this->assertTrue($m->hasErrors('attr_arr'));
 }
Ejemplo n.º 2
0
 public function testValidateAttribute()
 {
     // existing value on different table
     $val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
     $m = ValidatorTestRefModel::findOne(['id' => 1]);
     $val->validateAttribute($m, 'ref');
     $this->assertFalse($m->hasErrors());
     // non-existing value on different table
     $val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
     $m = ValidatorTestRefModel::findOne(['id' => 6]);
     $val->validateAttribute($m, 'ref');
     $this->assertTrue($m->hasErrors('ref'));
     // existing value on same table
     $val = new ExistValidator(['targetAttribute' => 'ref']);
     $m = ValidatorTestRefModel::findOne(['id' => 2]);
     $val->validateAttribute($m, 'test_val');
     $this->assertFalse($m->hasErrors());
     // non-existing value on same table
     $val = new ExistValidator(['targetAttribute' => 'ref']);
     $m = ValidatorTestRefModel::findOne(['id' => 5]);
     $val->validateAttribute($m, 'test_val_fail');
     $this->assertTrue($m->hasErrors('test_val_fail'));
     // check for given value (true)
     $val = new ExistValidator();
     $m = ValidatorTestRefModel::findOne(['id' => 3]);
     $val->validateAttribute($m, 'ref');
     $this->assertFalse($m->hasErrors());
     // check for given defaults (false)
     $val = new ExistValidator();
     $m = ValidatorTestRefModel::findOne(['id' => 4]);
     $m->a_field = 'some new value';
     $val->validateAttribute($m, 'a_field');
     $this->assertTrue($m->hasErrors('a_field'));
     // check array
     $val = new ExistValidator(['targetAttribute' => 'ref']);
     $m = ValidatorTestRefModel::findOne(['id' => 2]);
     $m->test_val = [1, 2, 3];
     $val->validateAttribute($m, 'test_val');
     $this->assertTrue($m->hasErrors('test_val'));
 }