Commit 68341dd5 by Alexander Makarov

Merge branch 'defaultValueValidator' of github.com:Alex-Code/yii2 into…

Merge branch 'defaultValueValidator' of github.com:Alex-Code/yii2 into Alex-Code-defaultValueValidator Conflicts: framework/CHANGELOG.md
parents da2381cb 7ff1f4ae
...@@ -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