From 4e0627232c284d697e4e61f41a076de42d2f2d2c Mon Sep 17 00:00:00 2001 From: AD7six Date: Wed, 19 Feb 2014 15:03:11 +0000 Subject: [PATCH] account for localized floats more robustly. Normalize floats, strings too, so that they are validated as dddddddddd.dd ref #2853 --- lib/Cake/Test/Case/Utility/ValidationTest.php | 9 ++++++--- lib/Cake/Utility/Validation.php | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 073fe792612..0d551746212 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1660,12 +1660,15 @@ public function testDecimalCustomRegex() { * @return void */ public function testDecimalLocaleSet() { - $this->skipIf(DS === '\\', 'The locale is not supported in Windows and affect the others tests.'); + $this->skipIf(DS === '\\', 'The locale is not supported in Windows and affects other tests.'); $restore = setlocale(LC_NUMERIC, 0); $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available."); - $this->assertTrue(Validation::decimal(1.54)); - $this->assertTrue(Validation::decimal('1.54')); + $this->assertTrue(Validation::decimal(1.54), '1.54 should be considered a valid float'); + $this->assertTrue(Validation::decimal('1.54'), '"1.54" should be considered a valid float'); + + $this->assertTrue(Validation::decimal(12345.67), '12345.67 should be considered a valid float'); + $this->assertTrue(Validation::decimal('12,345.67'), '"12,345.67" should be considered a valid float'); setlocale(LC_NUMERIC, $restore); } diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 14f06087c57..03d05207b13 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -426,10 +426,11 @@ public static function decimal($check, $places = null, $regex = null) { } } - // Workaround localized floats. - if (is_float($check)) { - $check = str_replace(',', '.', strval($check)); - } + // account for localized floats. + $data = localeconv(); + $check = str_replace($data['thousands_sep'], '', $check); + $check = str_replace($data['decimal_point'], '.', $check); + return self::_check($check, $regex); }