Skip to content

Commit 0307ae4

Browse files
authored
[10.x] Support ConditionalRules within NestedRules (#47344)
* failing test * filter conditional rules when compiling NestedRules * styleci * always filter conditional rules * modify test * split tests
1 parent d06f3a9 commit 0307ae4

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/Illuminate/Validation/NestedRules.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public function compile($attribute, $value, $data = null)
4747
$nested[$attribute.'.'.$key] = $rule;
4848
}
4949

50-
return $parser->explode($nested);
50+
$rules = $nested;
51+
} else {
52+
$rules = [$attribute => $rules];
5153
}
5254

53-
return $parser->explode([$attribute => $rules]);
55+
return $parser->explode(ValidationRuleParser::filterConditionalRules($rules, $data));
5456
}
5557
}

tests/Validation/ValidationForEachTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,70 @@ public function testForEachCallbacksCanContainMultipleRegexRules()
257257
], $v->getMessageBag()->toArray());
258258
}
259259

260+
public function testConditionalRulesCanBeAddedToForEachWithAssociativeArray()
261+
{
262+
$v = new Validator(
263+
$this->getIlluminateArrayTranslator(),
264+
[
265+
'foo' => [
266+
['bar' => true],
267+
['bar' => false],
268+
],
269+
],
270+
[
271+
'foo.*' => Rule::forEach(fn(mixed $value, string $attribute) => [
272+
'bar' => Rule::when(true, ['accepted'], ['declined']),
273+
]),
274+
]
275+
);
276+
277+
$this->assertEquals([
278+
'foo.1.bar' => ['validation.accepted'],
279+
], $v->getMessageBag()->toArray());
280+
}
281+
public function testConditionalRulesCanBeAddedToForEachWithList()
282+
{
283+
$v = new Validator(
284+
$this->getIlluminateArrayTranslator(),
285+
[
286+
'foo' => [
287+
['bar' => true],
288+
['bar' => false],
289+
],
290+
],
291+
[
292+
'foo.*.bar' => Rule::forEach(fn(mixed $value, string $attribute) => [
293+
Rule::when(true, ['accepted'], ['declined']),
294+
]),
295+
]);
296+
297+
$this->assertEquals([
298+
'foo.1.bar' => ['validation.accepted'],
299+
], $v->getMessageBag()->toArray());
300+
301+
}
302+
303+
public function testConditionalRulesCanBeAddedToForEachWithObject()
304+
{
305+
$v = new Validator(
306+
$this->getIlluminateArrayTranslator(),
307+
[
308+
'foo' => [
309+
['bar' => true],
310+
['bar' => false],
311+
],
312+
],
313+
[
314+
'foo.*.bar' => Rule::forEach(fn (mixed $value, string $attribute) =>
315+
Rule::when(true, ['accepted'], ['declined']),
316+
),
317+
]);
318+
319+
$this->assertEquals([
320+
'foo.1.bar' => ['validation.accepted'],
321+
], $v->getMessageBag()->toArray());
322+
}
323+
260324
protected function getTranslator()
261325
{
262326
return m::mock(TranslatorContract::class);

0 commit comments

Comments
 (0)