Skip to content

Commit 61bcf76

Browse files
fix: make required_with and required_without validation rule work with arrays
Closes #3965
1 parent 0cff548 commit 61bcf76

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

system/Validation/Rules.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,16 @@ public function required_with($str = null, string $fields, array $data): bool
376376
// as $fields is the lis
377377
$requiredFields = [];
378378

379-
foreach ($fields as $field)
380-
{
381-
if (array_key_exists($field, $data))
382-
{
379+
foreach ($fields as $field) {
380+
if (
381+
(strpos($field, '.') !== false &&
382+
!empty(dot_array_search($field, $data))) ||
383+
(array_key_exists($field, $data) && !empty($data[$field]))
384+
) {
383385
$requiredFields[] = $field;
384386
}
385387
}
386388

387-
// Remove any keys with empty values since, that means they
388-
// weren't truly there, as far as this is concerned.
389-
$requiredFields = array_filter($requiredFields, function ($item) use ($data) {
390-
return ! empty($data[$item]);
391-
});
392-
393389
return empty($requiredFields);
394390
}
395391

@@ -425,10 +421,13 @@ public function required_without($str = null, string $fields, array $data): bool
425421

426422
// Still here? Then we fail this test if
427423
// any of the fields are not present in $data
428-
foreach ($fields as $field)
429-
{
430-
if (! array_key_exists($field, $data))
431-
{
424+
foreach ($fields as $field) {
425+
if (
426+
(strpos($field, '.') !== false &&
427+
empty(dot_array_search($field, $data))) ||
428+
(strpos($field, '.') === false &&
429+
(!array_key_exists($field, $data) || empty($data[$field])))
430+
) {
432431
return false;
433432
}
434433
}

0 commit comments

Comments
 (0)