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..0399edb 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,9 @@ class Validation protected $aliases = []; protected $messageSeparator = ':'; + + protected $validData = []; + protected $invalidData = []; public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array()) { @@ -72,7 +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) { + $value = $ruleValidator->check(null); + $isEmptyValue = $this->isEmptyValue($value); + continue; + } + if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) { continue; } @@ -80,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) @@ -421,5 +438,19 @@ protected function resolveInputAttributes(array $inputs) return $resolvedInputs; } + + 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 1ef39ab..cda5353 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -104,6 +104,8 @@ protected function registerBaseValidators() 'callback' => new Rules\Callback, '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', + ]); + } }