Commit cc08492b by Qiang Xue

Fixes #2729: Added `FilterValidator::skipOnArray` so that filters like `trim`…

Fixes #2729: Added `FilterValidator::skipOnArray` so that filters like `trim` will not fail for array inputs
parent e5031dfd
...@@ -143,6 +143,7 @@ Yii Framework 2 Change Log ...@@ -143,6 +143,7 @@ Yii Framework 2 Change Log
- Enh #2646: Added support for specifying hostinfo in the pattern of a URL rule (qiangxue) - Enh #2646: Added support for specifying hostinfo in the pattern of a URL rule (qiangxue)
- Enh #2661: Added boolean column type support for SQLite (qiangxue) - Enh #2661: Added boolean column type support for SQLite (qiangxue)
- Enh #2670: Changed `console\Controller::globalOptions()` to `options($actionId)` to (make it possible to) differentiate options per action (hqx) - Enh #2670: Changed `console\Controller::globalOptions()` to `options($actionId)` to (make it possible to) differentiate options per action (hqx)
- Enh #2729: Added `FilterValidator::skipOnArray` so that filters like `trim` will not fail for array inputs (qiangxue)
- Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund) - Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund)
- Enh: Added support for using arrays as option values for console commands (qiangxue) - Enh: Added support for using arrays as option values for console commands (qiangxue)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
......
...@@ -40,6 +40,11 @@ class FilterValidator extends Validator ...@@ -40,6 +40,11 @@ class FilterValidator extends Validator
*/ */
public $filter; public $filter;
/** /**
* @var boolean whether the filter should be skipped if an array input is given.
* If false and an array input is given, the filter will not be applied.
*/
public $skipOnArray = false;
/**
* @var boolean this property is overwritten to be false so that this validator will * @var boolean this property is overwritten to be false so that this validator will
* be applied when the value being validated is empty. * be applied when the value being validated is empty.
*/ */
...@@ -61,6 +66,9 @@ class FilterValidator extends Validator ...@@ -61,6 +66,9 @@ class FilterValidator extends Validator
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
$object->$attribute = call_user_func($this->filter, $object->$attribute); $value = $object->$attribute;
if ($this->skipOnArray || !is_array($value)) {
$object->$attribute = call_user_func($this->filter, $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