Commit 7910cc11 by Qiang Xue

Merge pull request #2767 from ZhandosKz/filter_validator_skip_on_array

Fixed typo in FilterValidator for skipping array values.
parents 76a7beb6 202e3ae0
...@@ -67,7 +67,7 @@ class FilterValidator extends Validator ...@@ -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 ...@@ -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 ...@@ -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)
......
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