Skip to content

Commit

Permalink
Merge pull request #1742 from bangbangda/Fix-rules-bug
Browse files Browse the repository at this point in the history
Fix required_without rule bug.
  • Loading branch information
lonnieezell authored Feb 19, 2019
2 parents 5c11e90 + d322ac1 commit 711aec1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ public function required_without($str = null, string $fields, array $data): bool
// If the field is present we can safely assume that
// the field is here, no matter whether the corresponding
// search field is present or not.
$present = $this->required($data[$str] ?? null);
$present = $this->required($str ?? '');

if ($present === true)
if ($present)
{
return true;
}
Expand Down
24 changes: 17 additions & 7 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,23 +609,33 @@ for rules like ``require_with`` that needs to check the value of another submitt
// If the field is present we can safely assume that
// the field is here, no matter whether the corresponding
// search field is present or not.
$present = $this->required($data[$str] ?? null);
$present = $this->required($str ?? '');

if ($present === true)
if ($present)
{
return true;
}

// Still here? Then we fail this test if
// Still here? Then we fail this test if
// any of the fields are present in $data
$requiredFields = array_intersect($fields, $data);
// as $fields is the lis
$requiredFields = [];

$requiredFields = array_filter($requiredFields, function($item)
foreach ($fields as $field)
{
return ! empty($item);
if (array_key_exists($field, $data))
{
$requiredFields[] = $field;
}
}

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

return ! (bool)count($requiredFields);
return empty($requiredFields);
}

Custom errors can be returned as the fourth parameter, just as described above.
Expand Down

0 comments on commit 711aec1

Please sign in to comment.