Skip to content

Commit fbbd495

Browse files
authored
Merge pull request #14 from debba/fix-improve-validation-logic
fix: improve validation logic for boolean fields
2 parents 0b050d7 + d2b75e5 commit fbbd495

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Form.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
146146
$rule = $matches[0];
147147
}
148148

149-
if (in_array('optional', $rule) && empty($valueToTest)) {
149+
if (in_array('optional', $rule) && ($valueToTest === null || $valueToTest === '' || $valueToTest === [])) {
150150
return true;
151151
}
152152

@@ -175,7 +175,6 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
175175
$param = $ruleParams;
176176
}
177177

178-
179178
if (strpos($currentRule, ':') !== false && strpos($currentRule, '|') === false) {
180179
$ruleParts = explode(':', $currentRule);
181180

@@ -191,7 +190,8 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
191190
throw new \Exception("Rule $currentRule does not exist");
192191
}
193192

194-
if (!$valueToTest) {
193+
$isMissing = $valueToTest === null || $valueToTest === '' || ($valueToTest === []);
194+
if ($isMissing) {
195195
if ($expandedErrors) {
196196
$this->addError($fieldName, str_replace(
197197
['{field}', '{Field}', '{value}'],
@@ -247,6 +247,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
247247
$param = [$param];
248248
}
249249

250+
if (is_bool($valueToTest)) {
251+
$valueToTest = $valueToTest ? '1' : '0';
252+
}
253+
250254
if (is_float($valueToTest)) {
251255
$valueToTest = json_encode($valueToTest, JSON_PRESERVE_ZERO_FRACTION);
252256
}

0 commit comments

Comments
 (0)