Skip to content

Commit

Permalink
Merge pull request #2957 from najdanovicivan/validation-permit_empty-fix
Browse files Browse the repository at this point in the history
Validation - Handle required_with and required_without on permit_empty
  • Loading branch information
lonnieezell authored May 11, 2020
2 parents 0752ac4 + 8d30973 commit 3e405b9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
33 changes: 32 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,38 @@ protected function processRules(string $field, string $label = null, $value, $ru
{
if (! in_array('required', $rules) && (is_array($value) ? empty($value) : (trim($value) === '')))
{
return true;
$passed = true;

foreach ($rules as $rule)
{
if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
{
$rule = $match[1];
$param = $match[2];

if (! in_array($rule, ['required_with', 'required_without']))
{
continue;
}

// Check in our rulesets
foreach ($this->ruleSetInstances as $set)
{
if (! method_exists($set, $rule))
{
continue;
}

$passed = $passed && $set->$rule($value, $param, $data);
break;
}
}
}

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

$rules = array_diff($rules, ['permit_empty']);
Expand Down
60 changes: 60 additions & 0 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,66 @@ public function emptysProvider()
['foo' => 0.0],
true,
],
[
['foo' => 'permit_empty|required_with[bar]'],
['foo' => ''],
true,
],
[
['foo' => 'permit_empty|required_with[bar]'],
['foo' => 0],
true,
],
[
[
'foo' => 'permit_empty|required_with[bar]',
],
[
'foo' => 0.0,
'bar' => 1,
],
true,
],
[
[
'foo' => 'permit_empty|required_with[bar]',
],
[
'foo' => '',
'bar' => 1,
],
false,
],
[
['foo' => 'permit_empty|required_without[bar]'],
['foo' => ''],
false,
],
[
['foo' => 'permit_empty|required_without[bar]'],
['foo' => 0],
true,
],
[
[
'foo' => 'permit_empty|required_without[bar]',
],
[
'foo' => 0.0,
'bar' => 1,
],
true,
],
[
[
'foo' => 'permit_empty|required_without[bar]',
],
[
'foo' => '',
'bar' => 1,
],
true,
],
];
}

Expand Down

0 comments on commit 3e405b9

Please sign in to comment.