Commit 7dfd210b by Alex-Code

Update DefaultValueValidator.php

Allow the default value to be a closure
parent 4ccf0931
......@@ -19,7 +19,7 @@ namespace yii\validators;
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;
/**
......@@ -34,7 +34,11 @@ class DefaultValueValidator extends Validator
public function validateAttribute($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;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment