Skip to content

Commit 295c1ac

Browse files
authored
Merge pull request #3966 from benjaminbellamy/patch-1
fix: add dot_array_search to required_with and required_without
2 parents 5da7ab2 + 2077414 commit 295c1ac

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

system/Validation/Rules.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,15 @@ public function required_with($str = null, string $fields = null, array $data =
384384

385385
foreach ($fields as $field)
386386
{
387-
if (array_key_exists($field, $data))
387+
if (
388+
(array_key_exists($field, $data) && ! empty($data[$field])) ||
389+
(strpos($field, '.') !== false && ! empty(dot_array_search($field, $data)))
390+
)
388391
{
389392
$requiredFields[] = $field;
390393
}
391394
}
392395

393-
// Remove any keys with empty values since, that means they
394-
// weren't truly there, as far as this is concerned.
395-
$requiredFields = array_filter($requiredFields, function ($item) use ($data) {
396-
return ! empty($data[$item]);
397-
});
398-
399396
return empty($requiredFields);
400397
}
401398

@@ -438,7 +435,10 @@ public function required_without($str = null, string $fields = null, array $data
438435
// any of the fields are not present in $data
439436
foreach ($fields as $field)
440437
{
441-
if (! array_key_exists($field, $data))
438+
if (
439+
(strpos($field, '.') === false && (! array_key_exists($field, $data) || empty($data[$field]))) ||
440+
(strpos($field, '.') !== false && empty(dot_array_search($field, $data)))
441+
)
442442
{
443443
return false;
444444
}

tests/system/Validation/RulesTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ public function testRequiredWith($field, $check, $expected = false)
14991499
'foo' => 'bar',
15001500
'bar' => 'something',
15011501
'baz' => null,
1502-
'ar' => [],// Was running into issues with array values
1502+
'array' => ['nonEmptyField1'=>'value1','nonEmptyField2'=>'value2', 'emptyField1'=>null, 'emptyField2'=>null],
15031503
];
15041504

15051505
$this->validation->setRules([
@@ -1544,6 +1544,26 @@ public function requiredWithProvider()
15441544
null,
15451545
true,
15461546
],
1547+
[
1548+
'array.emptyField1',
1549+
'array.emptyField2',
1550+
true,
1551+
],
1552+
[
1553+
'array.nonEmptyField1',
1554+
'array.emptyField2',
1555+
true,
1556+
],
1557+
[
1558+
'array.emptyField1',
1559+
'array.nonEmptyField2',
1560+
false,
1561+
],
1562+
[
1563+
'array.nonEmptyField1',
1564+
'array.nonEmptyField2',
1565+
true,
1566+
],
15471567
];
15481568
}
15491569

@@ -1561,6 +1581,7 @@ public function testRequiredWithout($field, $check, $expected = false)
15611581
'foo' => 'bar',
15621582
'bar' => 'something',
15631583
'baz' => null,
1584+
'array' => ['nonEmptyField1'=>'value1','nonEmptyField2'=>'value2', 'emptyField1'=>null, 'emptyField2'=>null],
15641585
];
15651586

15661587
$this->validation->setRules([
@@ -1600,6 +1621,26 @@ public function requiredWithoutProvider()
16001621
null,
16011622
true,
16021623
],
1624+
[
1625+
'array.emptyField1',
1626+
'array.emptyField2',
1627+
false,
1628+
],
1629+
[
1630+
'array.nonEmptyField1',
1631+
'array.emptyField2',
1632+
true,
1633+
],
1634+
[
1635+
'array.emptyField1',
1636+
'array.nonEmptyField2',
1637+
true,
1638+
],
1639+
[
1640+
'array.nonEmptyField1',
1641+
'array.nonEmptyField2',
1642+
true,
1643+
],
16031644
];
16041645
}
16051646

0 commit comments

Comments
 (0)