Skip to content

Commit 84c6055

Browse files
Add validation test for forEach with null and empty array values (#55047)
1 parent 8f3f0fd commit 84c6055

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/Validation/ValidationForEachTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,37 @@ public function testConditionalRulesCanBeAddedToForEachWithObject()
319319
], $v->getMessageBag()->toArray());
320320
}
321321

322+
public function testForEachWithEmptyAndNullValues()
323+
{
324+
$data = [
325+
'items' => [
326+
['discounts' => null],
327+
['discounts' => []],
328+
['discounts' => [null]],
329+
],
330+
];
331+
332+
$rules = [
333+
'items.*' => Rule::forEach(function () {
334+
return [
335+
'discounts' => 'required|array',
336+
'discounts.*' => 'required|array',
337+
];
338+
}),
339+
];
340+
341+
$v = new Validator($this->getIlluminateArrayTranslator(), $data, $rules);
342+
$this->assertFalse($v->passes());
343+
$this->assertEquals(
344+
[
345+
'items.0.discounts' => ['validation.required'],
346+
'items.1.discounts' => ['validation.required'],
347+
'items.2.discounts.0' => ['validation.required'],
348+
],
349+
$v->getMessageBag()->toArray()
350+
);
351+
}
352+
322353
public function getIlluminateArrayTranslator()
323354
{
324355
return new Translator(

0 commit comments

Comments
 (0)