Skip to content

Commit

Permalink
fix: fix requireIf checked and notChecked conditional key implementat…
Browse files Browse the repository at this point in the history
…ions

This fix now considers when the checkbox field is sent, in the case of api requests. Web browsers do
not send unchecked checkbox fields
  • Loading branch information
teclone committed Oct 22, 2018
1 parent e67dfa3 commit f6cf020
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ protected function validateFields(array $fields, bool $required)
}
}

/**
* tests if a value is falsy
*
*@param $value - the value to test
*/
protected function valueIsFalsy($value)
{
$value = strval($value);

if (preg_match('/^(false|off|0|nil|null|no|undefined)$/i', $value) || $value === '')
return true;
else
return false;
}

/**
* runs data filteration on the given value
*
Expand Down Expand Up @@ -425,7 +440,7 @@ protected function filterValue($value, array $filters)
break;

case 'bool':
if (preg_match('/^(false|off|0|nil|null|no|undefined)$/i', $value) || $value === '')
if ($this->valueIsFalsy($value))
$value = false;
else
$value = true;
Expand Down Expand Up @@ -699,12 +714,12 @@ protected function processRules()
switch(strtolower($condition))
{
case 'checked':
if (!is_null($_field_value))
if (!$this->valueIsFalsy($_field_value))
$required = true;
break;

case 'notchecked':
if (is_null($_field_value))
if ($this->valueIsFalsy($_field_value))
$required = true;
break;

Expand Down

0 comments on commit f6cf020

Please sign in to comment.