mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 06:48:59 +08:00
Merge pull request #2767 from ZhandosKz/filter_validator_skip_on_array
Fixed typo in FilterValidator for skipping array values.
This commit is contained in:
@@ -67,7 +67,7 @@ class FilterValidator extends Validator
|
|||||||
public function validateAttribute($object, $attribute)
|
public function validateAttribute($object, $attribute)
|
||||||
{
|
{
|
||||||
$value = $object->$attribute;
|
$value = $object->$attribute;
|
||||||
if ($this->skipOnArray || !is_array($value)) {
|
if (!$this->skipOnArray || !is_array($value)) {
|
||||||
$object->$attribute = call_user_func($this->filter, $value);
|
$object->$attribute = call_user_func($this->filter, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ class FilterValidatorTest extends TestCase
|
|||||||
'attr_one' => ' to be trimmed ',
|
'attr_one' => ' to be trimmed ',
|
||||||
'attr_two' => 'set this to null',
|
'attr_two' => 'set this to null',
|
||||||
'attr_empty1' => '',
|
'attr_empty1' => '',
|
||||||
'attr_empty2' => null
|
'attr_empty2' => null,
|
||||||
|
'attr_array' => ['Maria', 'Anna', 'Elizabeth'],
|
||||||
|
'attr_array_skipped' => ['John', 'Bill']
|
||||||
]);
|
]);
|
||||||
$val = new FilterValidator(['filter' => 'trim']);
|
$val = new FilterValidator(['filter' => 'trim']);
|
||||||
$val->validateAttribute($m, 'attr_one');
|
$val->validateAttribute($m, 'attr_one');
|
||||||
@@ -42,6 +44,16 @@ class FilterValidatorTest extends TestCase
|
|||||||
$val->skipOnEmpty = true;
|
$val->skipOnEmpty = true;
|
||||||
$val->validateAttribute($m, 'attr_empty2');
|
$val->validateAttribute($m, 'attr_empty2');
|
||||||
$this->assertNotNull($m->attr_empty2);
|
$this->assertNotNull($m->attr_empty2);
|
||||||
|
$val->filter = function($value) {
|
||||||
|
|
||||||
|
return implode(',', $value);
|
||||||
|
};
|
||||||
|
$val->skipOnArray = false;
|
||||||
|
$val->validateAttribute($m, 'attr_array');
|
||||||
|
$this->assertSame('Maria,Anna,Elizabeth', $m->attr_array);
|
||||||
|
$val->skipOnArray = true;
|
||||||
|
$val->validateAttribute($m, 'attr_array_skipped');
|
||||||
|
$this->assertSame(['John', 'Bill'], $m->attr_array_skipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function notToBeNull($value)
|
public function notToBeNull($value)
|
||||||
|
|||||||
Reference in New Issue
Block a user