From e155920f09ee91001f51c90a9f62038cfc71865e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 29 Mar 2017 16:00:34 +0200 Subject: [PATCH] Validators::isInRange() compares strings as strings and numbers as numbers (BC break) [Closes nette/forms#146] --- src/Utils/Validators.php | 4 ++-- tests/Utils/Validators.isInRange().phpt | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Utils/Validators.php b/src/Utils/Validators.php index a67661faa..54b5f986f 100644 --- a/src/Utils/Validators.php +++ b/src/Utils/Validators.php @@ -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])); } diff --git a/tests/Utils/Validators.isInRange().phpt b/tests/Utils/Validators.isInRange().phpt index 53980d2fa..cb83ef593 100644 --- a/tests/Utils/Validators.isInRange().phpt +++ b/tests/Utils/Validators.isInRange().phpt @@ -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::false(Validators::isInRange(10, [NULL, 'a']));