From 28bd6880df2f4a6bd182bcb0f3a04dd5a1b64fad Mon Sep 17 00:00:00 2001 From: Cauan Cabral Date: Tue, 17 Apr 2012 10:49:41 -0400 Subject: [PATCH] Make Validation::decimal accept integers Fix #2800 Force locale of ValidationTests with en_US to ensure decimal dot separator --- lib/Cake/Test/Case/Utility/ValidationTest.php | 12 +++++++++--- lib/Cake/Utility/Validation.php | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 9763e9caa79..24c49c9a0b6 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -103,6 +103,8 @@ class ValidationTest extends CakeTestCase { public function setUp() { parent::setUp(); $this->_appEncoding = Configure::read('App.encoding'); + $this->_appLocale = setlocale(LC_ALL, "0"); + setlocale(LC_ALL, 'en_US'); } /** @@ -113,6 +115,7 @@ public function setUp() { public function tearDown() { parent::tearDown(); Configure::write('App.encoding', $this->_appEncoding); + setlocale(LC_ALL, $this->_appLocale); } /** @@ -1490,10 +1493,13 @@ public function testDecimal() { $this->assertTrue(Validation::decimal('+0123.45e6')); $this->assertTrue(Validation::decimal('-0123.45e6')); $this->assertTrue(Validation::decimal('0123.45e6')); + $this->assertTrue(Validation::decimal('1234')); + $this->assertTrue(Validation::decimal('-1234')); + $this->assertTrue(Validation::decimal('+1234')); + $this->assertTrue(Validation::decimal(1234.56)); + $this->assertTrue(Validation::decimal(1234.00)); + $this->assertFalse(Validation::decimal('string')); - $this->assertFalse(Validation::decimal('1234')); - $this->assertFalse(Validation::decimal('-1234')); - $this->assertFalse(Validation::decimal('+1234')); } /** diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 6934313edcd..29fecadfc07 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -382,9 +382,9 @@ public static function boolean($check) { public static function decimal($check, $places = null, $regex = null) { if (is_null($regex)) { if (is_null($places)) { - $regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/'; + $regex = '/^[-+]?[0-9]*(\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?)?$/'; } else { - $regex = '/^[-+]?[0-9]*\\.{1}[0-9]{' . $places . '}$/'; + $regex = '/^[-+]?[0-9]*(\\.{1}[0-9]{' . $places . '})?$/'; } } return self::_check($check, $regex);