Skip to content

Commit 4e06272

Browse files
committed
account for localized floats more robustly.
Normalize floats, strings too, so that they are validated as dddddddddd.dd ref #2853
1 parent 0a51458 commit 4e06272

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/Cake/Test/Case/Utility/ValidationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,12 +1660,15 @@ public function testDecimalCustomRegex() {
16601660
* @return void
16611661
*/
16621662
public function testDecimalLocaleSet() {
1663-
$this->skipIf(DS === '\\', 'The locale is not supported in Windows and affect the others tests.');
1663+
$this->skipIf(DS === '\\', 'The locale is not supported in Windows and affects other tests.');
16641664
$restore = setlocale(LC_NUMERIC, 0);
16651665
$this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
16661666

1667-
$this->assertTrue(Validation::decimal(1.54));
1668-
$this->assertTrue(Validation::decimal('1.54'));
1667+
$this->assertTrue(Validation::decimal(1.54), '1.54 should be considered a valid float');
1668+
$this->assertTrue(Validation::decimal('1.54'), '"1.54" should be considered a valid float');
1669+
1670+
$this->assertTrue(Validation::decimal(12345.67), '12345.67 should be considered a valid float');
1671+
$this->assertTrue(Validation::decimal('12,345.67'), '"12,345.67" should be considered a valid float');
16691672

16701673
setlocale(LC_NUMERIC, $restore);
16711674
}

lib/Cake/Utility/Validation.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ public static function decimal($check, $places = null, $regex = null) {
426426
}
427427
}
428428

429-
// Workaround localized floats.
430-
if (is_float($check)) {
431-
$check = str_replace(',', '.', strval($check));
432-
}
429+
// account for localized floats.
430+
$data = localeconv();
431+
$check = str_replace($data['thousands_sep'], '', $check);
432+
$check = str_replace($data['decimal_point'], '.', $check);
433+
433434
return self::_check($check, $regex);
434435
}
435436

0 commit comments

Comments
 (0)