Skip to content

Commit c57f604

Browse files
committed
Validator::validateNumeric() accepts unsigned integer [Closes #246]
1 parent 24b09bb commit c57f604

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Forms/Validator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ public static function validatePatternCaseInsensitive(IControl $control, string
284284
*/
285285
public static function validateNumeric(IControl $control): bool
286286
{
287-
return (bool) Strings::match($control->getValue(), '#^\d+$#D');
287+
$value = $control->getValue();
288+
return (is_int($value) && $value >= 0)
289+
|| (is_string($value) && Strings::match($value, '#^\d+$#D'));
288290
}
289291

290292

tests/Forms/Controls.TestBase.validators.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,25 @@ test(function () {
165165
Assert::true(Validator::validateNumeric($control));
166166
Assert::same('123', $control->value);
167167

168+
$control->value = 123;
169+
Assert::true(Validator::validateNumeric($control));
170+
Assert::same(123, $control->value);
171+
168172
$control->value = '0123';
169173
Assert::true(Validator::validateNumeric($control));
170174
Assert::same('0123', $control->value);
171175

172176
$control->value = '-123';
173177
Assert::false(Validator::validateNumeric($control));
174178

179+
$control->value = -123;
180+
Assert::false(Validator::validateNumeric($control));
181+
175182
$control->value = '123.5';
176183
Assert::false(Validator::validateNumeric($control));
184+
185+
$control->value = 123.5;
186+
Assert::false(Validator::validateNumeric($control));
177187
});
178188

179189

0 commit comments

Comments
 (0)