From dc79b611f28b6f59627bbd52cc55c6f226217fc9 Mon Sep 17 00:00:00 2001 From: zhouweiphp Date: Thu, 7 Jun 2018 19:41:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5default=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E8=AE=BE=E7=BD=AE=EF=BC=8C=E4=BB=A5=E5=8F=8A=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=AA=8C=E8=AF=81=E6=88=90=E5=8A=9F=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rules/Defaults.php | 22 ++++++++++++++++++++++ src/Validation.php | 13 +++++++++++++ src/Validator.php | 1 + 3 files changed, 36 insertions(+) create mode 100644 src/Rules/Defaults.php diff --git a/src/Rules/Defaults.php b/src/Rules/Defaults.php new file mode 100644 index 0000000..f089bf3 --- /dev/null +++ b/src/Rules/Defaults.php @@ -0,0 +1,22 @@ +requireParameters($this->fillable_params); + + $default = $this->parameter('default'); + return $default; + } + +} diff --git a/src/Validation.php b/src/Validation.php index 1e34cf0..94711d9 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -4,6 +4,7 @@ use Rakit\Validation\Rules\Required; use Closure; +use Rakit\Validation\Rules\Defaults; class Validation { @@ -19,6 +20,8 @@ class Validation protected $aliases = []; protected $messageSeparator = ':'; + + protected $validata = []; public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array()) { @@ -73,6 +76,12 @@ protected function validateAttribute(Attribute $attribute) $isEmptyValue = $this->isEmptyValue($value); foreach($rules as $ruleValidator) { + if ($isEmptyValue && $ruleValidator instanceof Defaults) { + $default = $ruleValidator->check(null); + $this->validata[$attributeKey] = $default; + } else { + $this->validata[$attributeKey] = $value; + } if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) { continue; } @@ -421,5 +430,9 @@ protected function resolveInputAttributes(array $inputs) return $resolvedInputs; } + + public function getValidata() { + return $this->validata; + } } diff --git a/src/Validator.php b/src/Validator.php index 1ef39ab..ae80a98 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -104,6 +104,7 @@ protected function registerBaseValidators() 'callback' => new Rules\Callback, 'before' => new Rules\Before, 'after' => new Rules\After, + 'defaults' => new Rules\Defaults, ]; foreach($baseValidator as $key => $validator) { From b12095980c6466eade038744b29d806013225480 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 8 Jun 2018 17:25:14 +0700 Subject: [PATCH 2/2] Correcting 'defaults' functionality, and add tests for it --- src/Validation.php | 34 ++++++++++++++++++++++-------- src/Validator.php | 1 + tests/Rules/DefaultsTest.php | 20 ++++++++++++++++++ tests/ValidatorTest.php | 40 ++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 tests/Rules/DefaultsTest.php diff --git a/src/Validation.php b/src/Validation.php index 94711d9..0399edb 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -21,7 +21,8 @@ class Validation protected $messageSeparator = ':'; - protected $validata = []; + protected $validData = []; + protected $invalidData = []; public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array()) { @@ -75,13 +76,14 @@ protected function validateAttribute(Attribute $attribute) $value = $this->getValue($attributeKey); $isEmptyValue = $this->isEmptyValue($value); + $isValid = true; foreach($rules as $ruleValidator) { if ($isEmptyValue && $ruleValidator instanceof Defaults) { - $default = $ruleValidator->check(null); - $this->validata[$attributeKey] = $default; - } else { - $this->validata[$attributeKey] = $value; + $value = $ruleValidator->check(null); + $isEmptyValue = $this->isEmptyValue($value); + continue; } + if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) { continue; } @@ -89,13 +91,19 @@ protected function validateAttribute(Attribute $attribute) $valid = $ruleValidator->check($value); if (!$valid) { + $isValid = false; $this->addError($attribute, $value, $ruleValidator); - if ($ruleValidator->isImplicit()) { break; } } } + + if ($isValid) { + $this->validData[$attributeKey] = $value; + } else { + $this->invalidData[$attributeKey] = $value; + } } protected function isArrayAttribute(Attribute $attribute) @@ -431,8 +439,18 @@ protected function resolveInputAttributes(array $inputs) return $resolvedInputs; } - public function getValidata() { - return $this->validata; + public function getValidatedData() { + return array_merge($this->validData, $this->invalidData); + } + + public function getValidData() + { + return $this->validData; + } + + public function getInvalidData() + { + return $this->invalidData; } } diff --git a/src/Validator.php b/src/Validator.php index ae80a98..cda5353 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -105,6 +105,7 @@ protected function registerBaseValidators() 'before' => new Rules\Before, 'after' => new Rules\After, 'defaults' => new Rules\Defaults, + 'default' => new Rules\Defaults, // alias of defaults ]; foreach($baseValidator as $key => $validator) { diff --git a/tests/Rules/DefaultsTest.php b/tests/Rules/DefaultsTest.php new file mode 100644 index 0000000..0bbde29 --- /dev/null +++ b/tests/Rules/DefaultsTest.php @@ -0,0 +1,20 @@ +rule = new Defaults; + } + + public function testDefaults() + { + $this->assertEquals($this->rule->fillParameters([10])->check(null), 10); + $this->assertEquals($this->rule->fillParameters(['something'])->check(null), 'something'); + $this->assertEquals($this->rule->fillParameters([[1,2,3]])->check('anything'), [1,2,3]); + } + +} diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index ed92db5..6cfedd9 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -709,4 +709,44 @@ public function testSetAttributeAliases() $this->assertEquals($errors->first('something:numeric'), 'Baz baz'); $this->assertEquals($errors->first('comments.0.text:required'), 'Qux qux'); } + + public function testUsingDefaults() + { + $validation = $this->validator->validate([ + 'is_active' => null, + 'is_published' => 'invalid-value' + ], [ + 'is_active' => 'defaults:0|required|in:0,1', + 'is_enabled' => 'defaults:1|required|in:0,1', + 'is_published' => 'required|in:0,1' + ]); + + $this->assertFalse($validation->passes()); + + $errors = $validation->errors(); + $this->assertNull($errors->first('is_active')); + $this->assertNull($errors->first('is_enabled')); + $this->assertNotNull($errors->first('is_published')); + + // Getting (all) validated data + $validatedData = $validation->getValidatedData(); + $this->assertEquals($validatedData, [ + 'is_active' => '0', + 'is_enabled' => '1', + 'is_published' => 'invalid-value' + ]); + + // Getting only valid data + $validData = $validation->getValidData(); + $this->assertEquals($validData, [ + 'is_active' => '0', + 'is_enabled' => '1' + ]); + + // Getting only invalid data + $invalidData = $validation->getInvalidData(); + $this->assertEquals($invalidData, [ + 'is_published' => 'invalid-value', + ]); + } }