Commit 2a6e064a by Alexander Makarov

Merge branch 'Alex-Code-defaultValueValidator'

# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
parents da2381cb 68341dd5
...@@ -58,6 +58,7 @@ Yii Framework 2 Change Log ...@@ -58,6 +58,7 @@ Yii Framework 2 Change Log
- Enh #3284: Added support for checking multiple ETags by `yii\filters\HttpCache` (qiangxue) - Enh #3284: Added support for checking multiple ETags by `yii\filters\HttpCache` (qiangxue)
- Enh #3298: Supported configuring `View::theme` using a class name (netyum, qiangxue) - Enh #3298: Supported configuring `View::theme` using a class name (netyum, qiangxue)
- Enh #3328: `BaseMailer` generates better text body from html body (armab) - Enh #3328: `BaseMailer` generates better text body from html body (armab)
- Enh #3380: Allow `value` in `defaultValueValidator` to be a closure (Alex-Code)
- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v) - Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v)
- Enh #3518: `yii\helpers\Html::encode()` now replaces invalid code sequences with "?" (DaSourcerer) - Enh #3518: `yii\helpers\Html::encode()` now replaces invalid code sequences with "?" (DaSourcerer)
- Enh #3521: Added `yii\filters\HttpCache::sessionCacheLimiter` (qiangxue) - Enh #3521: Added `yii\filters\HttpCache::sessionCacheLimiter` (qiangxue)
......
...@@ -19,7 +19,12 @@ namespace yii\validators; ...@@ -19,7 +19,12 @@ 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 PHP callable returning the default value or the default value to be set to the specified attributes.
* The function signature must be as follows,
*
* ~~~
* function foo($object, $attribute) {...return $value; }
* ~~~
*/ */
public $value; public $value;
/** /**
...@@ -34,7 +39,11 @@ class DefaultValueValidator extends Validator ...@@ -34,7 +39,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, $object, $attribute);
} 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