Fix Validation::decimal() not working with localized floats.

Use similar workarounds as DboSource::value() for accepting localized
floats.

Fixes #2853
This commit is contained in:
mark_story 2014-02-18 22:18:56 -05:00
parent 3d00ca9643
commit 0a51458ffd
2 changed files with 21 additions and 0 deletions

View file

@ -1654,6 +1654,22 @@ class ValidationTest extends CakeTestCase {
$this->assertFalse(Validation::decimal('.54321', null, '/^[-+]?[0-9]+(\\.[0-9]+)?$/s'));
}
/**
* Test localized floats with decimal.
*
* @return void
*/
public function testDecimalLocaleSet() {
$this->skipIf(DS === '\\', 'The locale is not supported in Windows and affect the others 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'));
setlocale(LC_NUMERIC, $restore);
}
/**
* testEmail method
*

View file

@ -425,6 +425,11 @@ class Validation {
$regex = "/^{$sign}{$dnum}{$exp}$/";
}
}
// Workaround localized floats.
if (is_float($check)) {
$check = str_replace(',', '.', strval($check));
}
return self::_check($check, $regex);
}