Skip to content

Commit

Permalink
account for localized floats more robustly.
Browse files Browse the repository at this point in the history
Normalize floats, strings too, so that they are validated as

    dddddddddd.dd

ref #2853
  • Loading branch information
AD7six committed Feb 19, 2014
1 parent 0a51458 commit 4e06272
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -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);
}

Expand Down

0 comments on commit 4e06272

Please sign in to comment.