Skip to content

Commit b120959

Browse files
committed
Correcting 'defaults' functionality, and add tests for it
1 parent dc79b61 commit b120959

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

src/Validation.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Validation
2121

2222
protected $messageSeparator = ':';
2323

24-
protected $validata = [];
24+
protected $validData = [];
25+
protected $invalidData = [];
2526

2627
public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array())
2728
{
@@ -75,27 +76,34 @@ protected function validateAttribute(Attribute $attribute)
7576
$value = $this->getValue($attributeKey);
7677
$isEmptyValue = $this->isEmptyValue($value);
7778

79+
$isValid = true;
7880
foreach($rules as $ruleValidator) {
7981
if ($isEmptyValue && $ruleValidator instanceof Defaults) {
80-
$default = $ruleValidator->check(null);
81-
$this->validata[$attributeKey] = $default;
82-
} else {
83-
$this->validata[$attributeKey] = $value;
82+
$value = $ruleValidator->check(null);
83+
$isEmptyValue = $this->isEmptyValue($value);
84+
continue;
8485
}
86+
8587
if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) {
8688
continue;
8789
}
8890

8991
$valid = $ruleValidator->check($value);
9092

9193
if (!$valid) {
94+
$isValid = false;
9295
$this->addError($attribute, $value, $ruleValidator);
93-
9496
if ($ruleValidator->isImplicit()) {
9597
break;
9698
}
9799
}
98100
}
101+
102+
if ($isValid) {
103+
$this->validData[$attributeKey] = $value;
104+
} else {
105+
$this->invalidData[$attributeKey] = $value;
106+
}
99107
}
100108

101109
protected function isArrayAttribute(Attribute $attribute)
@@ -431,8 +439,18 @@ protected function resolveInputAttributes(array $inputs)
431439
return $resolvedInputs;
432440
}
433441

434-
public function getValidata() {
435-
return $this->validata;
442+
public function getValidatedData() {
443+
return array_merge($this->validData, $this->invalidData);
444+
}
445+
446+
public function getValidData()
447+
{
448+
return $this->validData;
449+
}
450+
451+
public function getInvalidData()
452+
{
453+
return $this->invalidData;
436454
}
437455

438456
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ protected function registerBaseValidators()
105105
'before' => new Rules\Before,
106106
'after' => new Rules\After,
107107
'defaults' => new Rules\Defaults,
108+
'default' => new Rules\Defaults, // alias of defaults
108109
];
109110

110111
foreach($baseValidator as $key => $validator) {

tests/Rules/DefaultsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Rakit\Validation\Rules\Defaults;
4+
5+
class DefaultsTest extends PHPUnit_Framework_TestCase
6+
{
7+
8+
public function setUp()
9+
{
10+
$this->rule = new Defaults;
11+
}
12+
13+
public function testDefaults()
14+
{
15+
$this->assertEquals($this->rule->fillParameters([10])->check(null), 10);
16+
$this->assertEquals($this->rule->fillParameters(['something'])->check(null), 'something');
17+
$this->assertEquals($this->rule->fillParameters([[1,2,3]])->check('anything'), [1,2,3]);
18+
}
19+
20+
}

tests/ValidatorTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,44 @@ public function testSetAttributeAliases()
709709
$this->assertEquals($errors->first('something:numeric'), 'Baz baz');
710710
$this->assertEquals($errors->first('comments.0.text:required'), 'Qux qux');
711711
}
712+
713+
public function testUsingDefaults()
714+
{
715+
$validation = $this->validator->validate([
716+
'is_active' => null,
717+
'is_published' => 'invalid-value'
718+
], [
719+
'is_active' => 'defaults:0|required|in:0,1',
720+
'is_enabled' => 'defaults:1|required|in:0,1',
721+
'is_published' => 'required|in:0,1'
722+
]);
723+
724+
$this->assertFalse($validation->passes());
725+
726+
$errors = $validation->errors();
727+
$this->assertNull($errors->first('is_active'));
728+
$this->assertNull($errors->first('is_enabled'));
729+
$this->assertNotNull($errors->first('is_published'));
730+
731+
// Getting (all) validated data
732+
$validatedData = $validation->getValidatedData();
733+
$this->assertEquals($validatedData, [
734+
'is_active' => '0',
735+
'is_enabled' => '1',
736+
'is_published' => 'invalid-value'
737+
]);
738+
739+
// Getting only valid data
740+
$validData = $validation->getValidData();
741+
$this->assertEquals($validData, [
742+
'is_active' => '0',
743+
'is_enabled' => '1'
744+
]);
745+
746+
// Getting only invalid data
747+
$invalidData = $validation->getInvalidData();
748+
$this->assertEquals($invalidData, [
749+
'is_published' => 'invalid-value',
750+
]);
751+
}
712752
}

0 commit comments

Comments
 (0)