44
55use Rakit \Validation \Rules \Required ;
66use Closure ;
7+ use Rakit \Validation \Rules \Defaults ;
78
89class 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}
0 commit comments