Skip to content

Commit 9e252c0

Browse files
authored
Merge pull request #29 from zw2582/master
Add `defaults` rule to set attribute default value, and add methods to get validated data, valid data, or invalid data.
2 parents 6cad2f8 + b120959 commit 9e252c0

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

src/Rules/Defaults.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Rules;
4+
5+
use Rakit\Validation\Rule;
6+
7+
class Defaults extends Rule
8+
{
9+
10+
protected $message = "The :attribute default is :default";
11+
12+
protected $fillable_params = ['default'];
13+
14+
public function check($value)
15+
{
16+
$this->requireParameters($this->fillable_params);
17+
18+
$default = $this->parameter('default');
19+
return $default;
20+
}
21+
22+
}

src/Validation.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rakit\Validation\Rules\Required;
66
use Closure;
7+
use Rakit\Validation\Rules\Defaults;
78

89
class Validation
910
{
@@ -19,6 +20,9 @@ class Validation
1920
protected $aliases = [];
2021

2122
protected $messageSeparator = ':';
23+
24+
protected $validData = [];
25+
protected $invalidData = [];
2226

2327
public function __construct(Validator $validator, array $inputs, array $rules, array $messages = array())
2428
{
@@ -72,21 +76,34 @@ protected function validateAttribute(Attribute $attribute)
7276
$value = $this->getValue($attributeKey);
7377
$isEmptyValue = $this->isEmptyValue($value);
7478

79+
$isValid = true;
7580
foreach($rules as $ruleValidator) {
81+
if ($isEmptyValue && $ruleValidator instanceof Defaults) {
82+
$value = $ruleValidator->check(null);
83+
$isEmptyValue = $this->isEmptyValue($value);
84+
continue;
85+
}
86+
7687
if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) {
7788
continue;
7889
}
7990

8091
$valid = $ruleValidator->check($value);
8192

8293
if (!$valid) {
94+
$isValid = false;
8395
$this->addError($attribute, $value, $ruleValidator);
84-
8596
if ($ruleValidator->isImplicit()) {
8697
break;
8798
}
8899
}
89100
}
101+
102+
if ($isValid) {
103+
$this->validData[$attributeKey] = $value;
104+
} else {
105+
$this->invalidData[$attributeKey] = $value;
106+
}
90107
}
91108

92109
protected function isArrayAttribute(Attribute $attribute)
@@ -421,5 +438,19 @@ protected function resolveInputAttributes(array $inputs)
421438

422439
return $resolvedInputs;
423440
}
441+
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;
454+
}
424455

425456
}

src/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ protected function registerBaseValidators()
104104
'callback' => new Rules\Callback,
105105
'before' => new Rules\Before,
106106
'after' => new Rules\After,
107+
'defaults' => new Rules\Defaults,
108+
'default' => new Rules\Defaults, // alias of defaults
107109
];
108110

109111
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)