File tree Expand file tree Collapse file tree 4 files changed +50
-5
lines changed Expand file tree Collapse file tree 4 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -28,4 +28,11 @@ public function isInvalid() : bool;
28
28
* @return \Illuminate\Contracts\Support\MessageProvider
29
29
*/
30
30
public function getErrors () : MessageProvider ;
31
+
32
+ /**
33
+ * Returns the reasons why the validator failed.
34
+ *
35
+ * @return array
36
+ */
37
+ public function getValidationFailureReasons () : array ;
31
38
}
Original file line number Diff line number Diff line change @@ -34,6 +34,13 @@ protected function getMessages() : array
34
34
return [];
35
35
}
36
36
37
+ /**
38
+ * The validator.
39
+ *
40
+ * @return \Illuminate\Contracts\Validation\Validator
41
+ */
42
+ private $ validator ;
43
+
37
44
/**
38
45
* Returns an instance of the validator from our container.
39
46
*
@@ -52,11 +59,14 @@ private function getValidationFactory() : Factory
52
59
*/
53
60
private function getValidator () : Validator
54
61
{
55
- return $ this ->getValidationFactory ()->make (
56
- $ this ->getData (),
57
- $ this ->getRules (),
58
- $ this ->getMessages ()
59
- );
62
+ if (is_null ($ this ->validator )) {
63
+ $ this ->validator = $ this ->getValidationFactory ()->make (
64
+ $ this ->getData (),
65
+ $ this ->getRules (),
66
+ $ this ->getMessages ()
67
+ );
68
+ }
69
+ return $ this ->validator ;
60
70
}
61
71
62
72
/**
@@ -106,6 +116,19 @@ public function getErrors() : MessageProvider
106
116
return $ this ->getValidator ()->getMessageBag ();
107
117
}
108
118
119
+ /**
120
+ * Returns the reasons why the validator failed.
121
+ *
122
+ * @return array
123
+ */
124
+ public function getValidationFailureReasons () : array
125
+ {
126
+ if ($ this ->isInvalid ()) {
127
+ return $ this ->getValidator ()->failed ();
128
+ }
129
+ return [];
130
+ }
131
+
109
132
/**
110
133
* Save the model to the database.
111
134
*
Original file line number Diff line number Diff line change @@ -52,4 +52,13 @@ public function testValidateThrowsAValidationException()
52
52
$ model = new InvalidModel ();
53
53
$ model ->validate ();
54
54
}
55
+
56
+ public function testGetValidationFailureReasonsReturnsTheReasonsWhyTheValidationFailed ()
57
+ {
58
+ $ model = new InvalidModel ();
59
+ $ reasons = $ model ->getValidationFailureReasons ();
60
+
61
+ $ this ->assertTrue (array_key_exists ('email ' , $ reasons ));
62
+ $ this ->assertTrue (array_key_exists ('Required ' , $ reasons ['email ' ]));
63
+ }
55
64
}
Original file line number Diff line number Diff line change @@ -33,4 +33,10 @@ public function testValidateDoesNotThrowAValidationException()
33
33
$ model = new ValidModel ();
34
34
$ this ->assertVoid ($ model ->validate ());
35
35
}
36
+
37
+ public function testGetValidationFailureReasonsReturnsAnEmptyArray ()
38
+ {
39
+ $ model = new ValidModel ();
40
+ $ this ->assertEmpty ($ model ->getValidationFailureReasons ());
41
+ }
36
42
}
You can’t perform that action at this time.
0 commit comments