@@ -139,6 +139,8 @@ public function __construct()
139139
140140 protected function test ($ rule , $ valueToTest , $ fieldName = 'item ' ): bool
141141 {
142+ $ expandedErrors = false ;
143+
142144 if (is_string ($ rule )) {
143145 $ rule = preg_match_all ('/[^|<>]+(?:<[^>]+>)?/ ' , $ rule , $ matches );
144146 $ rule = $ matches [0 ];
@@ -148,6 +150,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
148150 return true ;
149151 }
150152
153+ if (in_array ('expanded ' , $ rule )) {
154+ $ expandedErrors = true ;
155+ }
156+
151157 foreach ($ rule as $ currentRule ) {
152158 $ param = [];
153159
@@ -157,6 +163,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
157163 continue ;
158164 }
159165
166+ if ($ currentRule === 'expanded ' ) {
167+ continue ;
168+ }
169+
160170 if (preg_match ('/^[a-zA-Z]+<(.*(\|.*)*)>$/ ' , $ currentRule )) {
161171 $ ruleParts = explode ('< ' , $ currentRule );
162172 $ ruleParams = str_replace ('> ' , '' , $ ruleParts [1 ]);
@@ -182,11 +192,19 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
182192 }
183193
184194 if (!$ valueToTest ) {
185- $ this ->addError ($ fieldName , str_replace (
186- ['{field} ' , '{Field} ' , '{value} ' ],
187- [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
188- $ this ->messages ['required ' ] ?? '{Field} is invalid! '
189- ));
195+ if ($ expandedErrors ) {
196+ $ this ->addError ($ fieldName , str_replace (
197+ ['{field} ' , '{Field} ' , '{value} ' ],
198+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
199+ $ this ->messages ['required ' ] ?? '{Field} is invalid! '
200+ ));
201+ } else {
202+ $ this ->errors [$ fieldName ] = str_replace (
203+ ['{field} ' , '{Field} ' , '{value} ' ],
204+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
205+ $ this ->messages ['required ' ] ?? '{Field} is invalid! '
206+ );
207+ }
190208
191209 return false ;
192210 }
@@ -201,14 +219,25 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
201219 $ param = [$ param ];
202220 }
203221
204- $ this ->addError ($ fieldName , sprintf (
205- str_replace (
206- ['{field} ' , '{Field} ' , '{value} ' ],
207- [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
208- $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
209- ),
210- ...$ param ,
211- ));
222+ if ($ expandedErrors ) {
223+ $ this ->addError ($ fieldName , sprintf (
224+ str_replace (
225+ ['{field} ' , '{Field} ' , '{value} ' ],
226+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
227+ $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
228+ ),
229+ ...$ param ,
230+ ));
231+ } else {
232+ $ this ->errors [$ fieldName ] = sprintf (
233+ str_replace (
234+ ['{field} ' , '{Field} ' , '{value} ' ],
235+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
236+ $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
237+ ),
238+ ...$ param ,
239+ );
240+ }
212241 }
213242
214243 continue ;
@@ -228,14 +257,25 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
228257 FILTER_VALIDATE_BOOLEAN
229258 )
230259 ) {
231- $ this ->addError ($ fieldName , sprintf (
232- str_replace (
233- ['{field} ' , '{Field} ' , '{value} ' ],
234- [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
235- $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
236- ),
237- ...$ param ,
238- ));
260+ if ($ expandedErrors ) {
261+ $ this ->addError ($ fieldName , sprintf (
262+ str_replace (
263+ ['{field} ' , '{Field} ' , '{value} ' ],
264+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
265+ $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
266+ ),
267+ ...$ param ,
268+ ));
269+ } else {
270+ $ this ->errors [$ fieldName ] = sprintf (
271+ str_replace (
272+ ['{field} ' , '{Field} ' , '{value} ' ],
273+ [$ fieldName , ucfirst ($ fieldName ), is_array ($ valueToTest ) ? json_encode ($ valueToTest ) : $ valueToTest ],
274+ $ this ->messages [$ currentRule ] ?? '{Field} is invalid! '
275+ ),
276+ ...$ param ,
277+ );
278+ }
239279 }
240280 }
241281
0 commit comments