Skip to content

Commit dda17ec

Browse files
hans-thomasonlimetaylorotwell
authored
[10.x] Fix validation of attributes that depend on previous excluded attribute (#48122)
* fix validation of attributes that depend on others with exclude rule applied * fixed style * Validator's passes methods fixed; * tests added for passes method; * Apply fixes from StyleCI; * rename the test; * formatting --------- Co-authored-by: Philip Iezzi <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 901ca01 commit dda17ec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ public function passes()
437437
$this->validateAttribute($attribute, $rule);
438438

439439
if ($this->shouldBeExcluded($attribute)) {
440-
$this->removeAttribute($attribute);
441-
442440
break;
443441
}
444442

@@ -448,6 +446,12 @@ public function passes()
448446
}
449447
}
450448

449+
foreach ($this->rules as $attribute => $rules) {
450+
if ($this->shouldBeExcluded($attribute)) {
451+
$this->removeAttribute($attribute);
452+
}
453+
}
454+
451455
// Here we will spin through all of the "after" hooks on this validator and
452456
// fire them off. This gives the callbacks a chance to perform all kinds
453457
// of other validation that needs to get wrapped up in this operation.

tests/Validation/ValidationValidatorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8257,6 +8257,39 @@ public function testExclude($rules, $data, $expectedValidatedData)
82578257
$this->assertSame($expectedValidatedData, $validator->validated());
82588258
}
82598259

8260+
public function testExcludeBeforeADependentRule()
8261+
{
8262+
$validator = new Validator(
8263+
$this->getIlluminateArrayTranslator(),
8264+
[
8265+
'profile_id' => null,
8266+
'type' => 'denied',
8267+
],
8268+
[
8269+
'type' => ['required', 'string', 'exclude'],
8270+
'profile_id' => ['nullable', 'required_if:type,profile', 'integer'],
8271+
],
8272+
);
8273+
8274+
$this->assertTrue($validator->passes());
8275+
$this->assertSame(['profile_id' => null], $validator->validated());
8276+
8277+
$validator = new Validator(
8278+
$this->getIlluminateArrayTranslator(),
8279+
[
8280+
'profile_id' => null,
8281+
'type' => 'profile',
8282+
],
8283+
[
8284+
'type' => ['required', 'string', 'exclude'],
8285+
'profile_id' => ['nullable', 'required_if:type,profile', 'integer'],
8286+
],
8287+
);
8288+
8289+
$this->assertFalse($validator->passes());
8290+
$this->assertSame(['profile_id' => ['validation.required_if']], $validator->getMessageBag()->getMessages());
8291+
}
8292+
82608293
public function testExcludingArrays()
82618294
{
82628295
$validator = new Validator(

0 commit comments

Comments
 (0)