vlucas/valitron

Validate existing numeric with 0.00

Closed this issue · 1 comments

I can't seem to get the rule working. I want a field to be required but ONLY as existing and if existing just needs to be numeric where a value of 0.00 IS VALID.

Thank you.

I have tried required with true flag and numeric rule but it kicks back value with 0.00

The following should do it:

$validator = new \Valitron\Validator([
    'my_key' => 0.00,
]);
//key can be missing, but if it's set it must pass validation
$validator->rule('optional', 'my_key');
//@see https://www.php.net/is_numeric
$validator->rule('numeric', 'my_key');


$validator->validate(); //returns true