Skip to content

Validator class method validateField() $dataPresent handling ignores boolean values causing issues with rules based on null interpretation of field values #47

@mlatzko

Description

@mlatzko

Code causing the issue

// If there is data, and the data is not empty and not numeric. This allows for strings such as '0' to be passed
// as valid values.
$dataPresent = isset($data[$field]) && ! (empty($data[$field]) && ! is_numeric($data[$field]));

See: https://github.com/fuelphp/validation/blob/php7/src/Validator.php#L253

Description

The condition to populate $dataPresent will be set to false value because an "empty(false)" is true. This leads to further issue because the value of the field becomes transformed from "false" to "null". This than leads to issues with rules based on "null" interpretation like "Fuel\Validation\Rule\Required". The problem only occurs while a value of a field is Boolean "false".

Example

$ruleProvider = new \Fuel\Validation\RuleProvider\FromArray;
$validator    = new \Fuel\Validation\Validator;

$rules          = array('myBooleanVariable' => array('required'));
$dataToValidate = array('myBooleanVariable' => false);

$ruleProvider->setData($rules)->populateValidator($validator);

$result = $validator->run($dataToValidate);

$result->getErrors(); // array(1) {'myBooleanVariable' => string(49) "The field is required and has not been specified."}

Possible solution (adding check for Boolean values)

// If there is data, and the data is not empty and not numeric nor boolean. This allows for strings such as '0' to be passed
// as valid values.
$dataPresent = isset($data[$field]) && ! (empty($data[$field]) && ! is_numeric($data[$field]) && ! is_bool($data[$field]));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions