Update DefaultValueValidator.php

Allow the default value to be a closure
This commit is contained in:
Alex-Code
2014-05-06 15:56:02 +01:00
parent 4ccf093193
commit 7dfd210b79

View File

@ -19,7 +19,7 @@ namespace yii\validators;
class DefaultValueValidator extends Validator class DefaultValueValidator extends Validator
{ {
/** /**
* @var mixed the default value to be set to the specified attributes. * @var mixed a closure returning the default value or the default value to be set to the specified attributes.
*/ */
public $value; public $value;
/** /**
@ -34,7 +34,11 @@ class DefaultValueValidator extends Validator
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
if ($this->isEmpty($object->$attribute)) { if ($this->isEmpty($object->$attribute)) {
$object->$attribute = $this->value; if ($this->value instanceof \Closure) {
$object->$attribute = call_user_func($this->value);
} else {
$object->$attribute = $this->value;
}
} }
} }
} }