Skip to content

Commit

Permalink
Validators::isInRange() compares strings as strings and numbers as nu…
Browse files Browse the repository at this point in the history
…mbers (BC break) [Closes nette/forms#146]
  • Loading branch information
dg committed Mar 29, 2017
1 parent b3be7ea commit 296ca37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Utils/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ public static function isList($value): bool
public static function isInRange($value, array $range): bool
{
return $value !== NULL
&& (!isset($range[0]) || $range[0] === '' || $value >= $range[0])
&& (!isset($range[1]) || $range[1] === '' || $value <= $range[1]);
&& (!isset($range[0]) || (is_string($range[0]) ? (string) $value >= $range[0] : is_numeric($value) && $value * 1 >= $range[0]))
&& (!isset($range[1]) || (is_string($range[1]) ? (string) $value <= $range[1] : is_numeric($value) && $value * 1 <= $range[1]));
}


Expand Down
14 changes: 13 additions & 1 deletion tests/Utils/Validators.isInRange().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ Assert::true(Validators::isInRange(-1, [NULL, 2]));
Assert::true(Validators::isInRange(-1, ['', 2]));

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

Assert::false(Validators::isInRange(NULL, [0, 1]));
Assert::false(Validators::isInRange(NULL, ['0', 'b']));

Assert::true(Validators::isInRange('', ['', '']));
Assert::true(Validators::isInRange('', ['', 'b']));
Assert::false(Validators::isInRange('', ['a', 'b']));

Assert::false(Validators::isInRange('', [0, 1]));
Assert::false(Validators::isInRange('', [0, 1]));
Assert::false(Validators::isInRange('a', [1, NULL]));
Assert::false(Validators::isInRange('a', [NULL, 9]));
Assert::true(Validators::isInRange('1', [NULL, 9]));
Assert::false(Validators::isInRange(10, ['a', NULL]));
Assert::true(Validators::isInRange(10, [NULL, 'a']));

0 comments on commit 296ca37

Please sign in to comment.