From d2b75e57a4efa53ef34fa9b796810957d26fae56 Mon Sep 17 00:00:00 2001 From: Andrea Debernardi Date: Fri, 22 Aug 2025 12:42:00 +0200 Subject: [PATCH] fix: improve validation logic for optional fields and boolean handling - Fix optional field validation to properly handle null, empty string, and empty array - Add explicit boolean value conversion to string for validation - Improve missing value detection with strict type checking --- src/Form.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Form.php b/src/Form.php index 227a0ae..108867d 100644 --- a/src/Form.php +++ b/src/Form.php @@ -146,7 +146,7 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool $rule = $matches[0]; } - if (in_array('optional', $rule) && empty($valueToTest)) { + if (in_array('optional', $rule) && ($valueToTest === null || $valueToTest === '' || $valueToTest === [])) { return true; } @@ -175,7 +175,6 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool $param = $ruleParams; } - if (strpos($currentRule, ':') !== false && strpos($currentRule, '|') === false) { $ruleParts = explode(':', $currentRule); @@ -191,7 +190,8 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool throw new \Exception("Rule $currentRule does not exist"); } - if (!$valueToTest) { + $isMissing = $valueToTest === null || $valueToTest === '' || ($valueToTest === []); + if ($isMissing) { if ($expandedErrors) { $this->addError($fieldName, str_replace( ['{field}', '{Field}', '{value}'], @@ -247,6 +247,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool $param = [$param]; } + if (is_bool($valueToTest)) { + $valueToTest = $valueToTest ? '1' : '0'; + } + if (is_float($valueToTest)) { $valueToTest = json_encode($valueToTest, JSON_PRESERVE_ZERO_FRACTION); }