Skip to content

Commit

Permalink
[10.x] fix Validator::validated get nullable array (#50056)
Browse files Browse the repository at this point in the history
* fix Validator::validated get nullable array

* Update Validator.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
helitik and taylorotwell authored Feb 12, 2024
1 parent 7ff37c6 commit 52305ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,15 @@ public function validated()
$missingValue = new stdClass;

foreach ($this->getRules() as $key => $rules) {
$value = data_get($this->getData(), $key, $missingValue);

if ($this->excludeUnvalidatedArrayKeys &&
in_array('array', $rules) &&
$value !== null &&
! empty(preg_grep('/^'.preg_quote($key, '/').'\.+/', array_keys($this->getRules())))) {
continue;
}

$value = data_get($this->getData(), $key, $missingValue);

if ($value !== $missingValue) {
Arr::set($results, $key, $value);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ public function testNullable()
$this->assertSame('validation.boolean', $v->messages()->get('b')[0]);
}

public function testArrayNullableWithUnvalidatedArrayKeys()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, [
'x' => null,
], [
'x' => 'array|nullable',
'x.key' => 'string',
]);
$this->assertTrue($v->passes());
$this->assertArrayHasKey('x', $v->validated());

$v = new Validator($trans, [
'x' => null,
], [
'x' => 'array',
'x.key' => 'string',
]);
$this->assertFalse($v->passes());
}

public function testNullableMakesNoDifferenceIfImplicitRuleExists()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 52305ed

Please sign in to comment.