Skip to content

Commit

Permalink
[10.x] Fix validation of attributes that depend on previous excluded …
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
3 people authored Aug 27, 2023
1 parent 901ca01 commit dda17ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,6 @@ public function passes()
$this->validateAttribute($attribute, $rule);

if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);

break;
}

Expand All @@ -448,6 +446,12 @@ public function passes()
}
}

foreach ($this->rules as $attribute => $rules) {
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);
}
}

// Here we will spin through all of the "after" hooks on this validator and
// fire them off. This gives the callbacks a chance to perform all kinds
// of other validation that needs to get wrapped up in this operation.
Expand Down
33 changes: 33 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8257,6 +8257,39 @@ public function testExclude($rules, $data, $expectedValidatedData)
$this->assertSame($expectedValidatedData, $validator->validated());
}

public function testExcludeBeforeADependentRule()
{
$validator = new Validator(
$this->getIlluminateArrayTranslator(),
[
'profile_id' => null,
'type' => 'denied',
],
[
'type' => ['required', 'string', 'exclude'],
'profile_id' => ['nullable', 'required_if:type,profile', 'integer'],
],
);

$this->assertTrue($validator->passes());
$this->assertSame(['profile_id' => null], $validator->validated());

$validator = new Validator(
$this->getIlluminateArrayTranslator(),
[
'profile_id' => null,
'type' => 'profile',
],
[
'type' => ['required', 'string', 'exclude'],
'profile_id' => ['nullable', 'required_if:type,profile', 'integer'],
],
);

$this->assertFalse($validator->passes());
$this->assertSame(['profile_id' => ['validation.required_if']], $validator->getMessageBag()->getMessages());
}

public function testExcludingArrays()
{
$validator = new Validator(
Expand Down

0 comments on commit dda17ec

Please sign in to comment.