Skip to content

Commit 296ca37

Browse files
committedMar 29, 2017
Validators::isInRange() compares strings as strings and numbers as numbers (BC break) [Closes nette/forms#146]
1 parent b3be7ea commit 296ca37

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
 

‎src/Utils/Validators.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public static function isList($value): bool
239239
public static function isInRange($value, array $range): bool
240240
{
241241
return $value !== NULL
242-
&& (!isset($range[0]) || $range[0] === '' || $value >= $range[0])
243-
&& (!isset($range[1]) || $range[1] === '' || $value <= $range[1]);
242+
&& (!isset($range[0]) || (is_string($range[0]) ? (string) $value >= $range[0] : is_numeric($value) && $value * 1 >= $range[0]))
243+
&& (!isset($range[1]) || (is_string($range[1]) ? (string) $value <= $range[1] : is_numeric($value) && $value * 1 <= $range[1]));
244244
}
245245

246246

‎tests/Utils/Validators.isInRange().phpt

+13-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ Assert::true(Validators::isInRange(-1, [NULL, 2]));
2828
Assert::true(Validators::isInRange(-1, ['', 2]));
2929

3030
Assert::true(Validators::isInRange(1, [-1, NULL]));
31-
Assert::true(Validators::isInRange(1, [-1, '']));
31+
Assert::false(Validators::isInRange(1, [-1, '']));
3232

3333
Assert::false(Validators::isInRange(NULL, [0, 1]));
3434
Assert::false(Validators::isInRange(NULL, ['0', 'b']));
35+
36+
Assert::true(Validators::isInRange('', ['', '']));
37+
Assert::true(Validators::isInRange('', ['', 'b']));
38+
Assert::false(Validators::isInRange('', ['a', 'b']));
39+
40+
Assert::false(Validators::isInRange('', [0, 1]));
41+
Assert::false(Validators::isInRange('', [0, 1]));
42+
Assert::false(Validators::isInRange('a', [1, NULL]));
43+
Assert::false(Validators::isInRange('a', [NULL, 9]));
44+
Assert::true(Validators::isInRange('1', [NULL, 9]));
45+
Assert::false(Validators::isInRange(10, ['a', NULL]));
46+
Assert::true(Validators::isInRange(10, [NULL, 'a']));

0 commit comments

Comments
 (0)
Failed to load comments.