yii\validators\DefaultValueValidator::validateAttribute PHP Method

validateAttribute() public method

public validateAttribute ( $model, $attribute )
    public function validateAttribute($model, $attribute)
    {
        if ($this->isEmpty($model->{$attribute})) {
            if ($this->value instanceof \Closure) {
                $model->{$attribute} = call_user_func($this->value, $model, $attribute);
            } else {
                $model->{$attribute} = $this->value;
            }
        }
    }

Usage Example

 public function testValidateAttribute()
 {
     $val = new DefaultValueValidator();
     $val->value = 'test_value';
     $obj = new \stdclass();
     $obj->attrA = 'attrA';
     $obj->attrB = null;
     $obj->attrC = '';
     // original values to chek which attritubes where modified
     $objB = clone $obj;
     $val->validateAttribute($obj, 'attrB');
     $this->assertEquals($val->value, $obj->attrB);
     $this->assertEquals($objB->attrA, $obj->attrA);
     $val->value = 'new_test_value';
     $obj = clone $objB;
     // get clean object
     $val->validateAttribute($obj, 'attrC');
     $this->assertEquals('new_test_value', $obj->attrC);
     $this->assertEquals($objB->attrA, $obj->attrA);
     $val->validateAttribute($obj, 'attrA');
     $this->assertEquals($objB->attrA, $obj->attrA);
 }
DefaultValueValidator