Skip to content

Commit

Permalink
Validation - Handle 'required_with' and 'required_without' on 'permit…
Browse files Browse the repository at this point in the history
…_empty'
  • Loading branch information
najdanovicivan committed May 8, 2020
1 parent 85cd56a commit 8d30973
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 @@ -284,6 +284,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 8d30973

Please sign in to comment.