@@ -305,17 +305,50 @@ public function testRunReturnsLocalizedErrors(): void
305305
306306 public function testRunWithCustomErrors (): void
307307 {
308- $ data = ['foo ' => 'notanumber ' ];
309-
308+ $ data = [
309+ 'foo ' => 'notanumber ' ,
310+ 'bar ' => 'notanumber ' ,
311+ ];
310312 $ messages = [
311313 'foo ' => [
312314 'is_numeric ' => 'Nope. Not a number. ' ,
313315 ],
316+ 'bar ' => [
317+ 'is_numeric ' => 'No. Not a number. ' ,
318+ ],
314319 ];
320+ $ this ->validation ->setRules (['foo ' => 'is_numeric ' , 'bar ' => 'is_numeric ' ], $ messages );
321+ $ this ->validation ->run ($ data );
315322
316- $ this ->validation ->setRules (['foo ' => 'is_numeric ' ], $ messages );
323+ $ this ->assertSame ('Nope. Not a number. ' , $ this ->validation ->getError ('foo ' ));
324+ $ this ->assertSame ('No. Not a number. ' , $ this ->validation ->getError ('bar ' ));
325+ }
326+
327+ /**
328+ * @see https://github.com/codeigniter4/CodeIgniter4/issues/6239
329+ */
330+ public function testSetRuleWithCustomErrors (): void
331+ {
332+ $ data = [
333+ 'foo ' => 'notanumber ' ,
334+ 'bar ' => 'notanumber ' ,
335+ ];
336+ $ this ->validation ->setRule (
337+ 'foo ' ,
338+ 'Foo ' ,
339+ ['foo ' => 'is_numeric ' ],
340+ ['is_numeric ' => 'Nope. Not a number. ' ]
341+ );
342+ $ this ->validation ->setRule (
343+ 'bar ' ,
344+ 'Bar ' ,
345+ ['bar ' => 'is_numeric ' ],
346+ ['is_numeric ' => 'Nope. Not a number. ' ]
347+ );
317348 $ this ->validation ->run ($ data );
349+
318350 $ this ->assertSame ('Nope. Not a number. ' , $ this ->validation ->getError ('foo ' ));
351+ $ this ->assertSame ('Nope. Not a number. ' , $ this ->validation ->getError ('bar ' ));
319352 }
320353
321354 public function testCheck (): void
@@ -1098,6 +1131,12 @@ public function validationArrayDataCaseProvider(): iterable
10981131 ['foo ' => ['boz ' ]],
10991132 ]],
11001133 ];
1134+
1135+ yield 'leading-asterisk ' => [
1136+ true ,
1137+ ['*.foo ' => 'required ' ],
1138+ [['foo ' => 'bar ' ]],
1139+ ];
11011140 }
11021141
11031142 /**
@@ -1324,4 +1363,17 @@ public function testNestedArrayThrowsException(): void
13241363 'beneficiaries_accounts.account_2.purpose ' => 'The PURPOSE field must be at least 3 characters in length. ' ,
13251364 ], $ this ->validation ->getErrors ());
13261365 }
1366+
1367+ public function testRuleWithLeadingAsterisk (): void
1368+ {
1369+ $ data = [
1370+ ['foo ' => 1 ],
1371+ ['foo ' => null ],
1372+ ];
1373+
1374+ $ this ->validation ->setRules (['*.foo ' => 'required ' ], ['1.foo ' => ['required ' => 'Required {field} ' ]]);
1375+
1376+ $ this ->assertFalse ($ this ->validation ->run ($ data ));
1377+ $ this ->assertSame ('Required *.foo ' , $ this ->validation ->getError ('*.foo ' ));
1378+ }
13271379}
0 commit comments