From 50e3307f005ddcac59698ea125174649c3edf2e2 Mon Sep 17 00:00:00 2001 From: spiliot Date: Fri, 19 Apr 2013 01:58:08 +0300 Subject: [PATCH 1/3] Money validation accepts one decimal place --- lib/Cake/Utility/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index accd571df..735f7d94a 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -528,7 +528,7 @@ class Validation { * @return boolean Success */ public static function money($check, $symbolPosition = 'left') { - $money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?'; + $money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{1})?'; if ($symbolPosition === 'right') { $regex = '/^' . $money . '(? Date: Mon, 22 Apr 2013 03:42:19 +0300 Subject: [PATCH 2/3] Added tests --- lib/Cake/Test/Case/Utility/ValidationTest.php | 17 +++++++++++++---- lib/Cake/Utility/Validation.php | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 2af075603..ee0de0b18 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1958,29 +1958,38 @@ class ValidationTest extends CakeTestCase { * @return void */ public function testMoney() { + $this->assertTrue(Validation::money('100')); + $this->assertTrue(Validation::money('100.11')); + $this->assertTrue(Validation::money('100.112')); + $this->assertTrue(Validation::money('100.1')); + $this->assertTrue(Validation::money('100.111,1')); + $this->assertTrue(Validation::money('100.111,11')); + $this->assertFalse(Validation::money('100.111,111')); + $this->assertFalse(Validation::money('text')); + $this->assertTrue(Validation::money('$100')); $this->assertTrue(Validation::money('$100.11')); $this->assertTrue(Validation::money('$100.112')); - $this->assertFalse(Validation::money('$100.1')); + $this->assertTrue(Validation::money('$100.1')); $this->assertFalse(Validation::money('$100.1111')); $this->assertFalse(Validation::money('text')); $this->assertTrue(Validation::money('100', 'right')); $this->assertTrue(Validation::money('100.11$', 'right')); $this->assertTrue(Validation::money('100.112$', 'right')); - $this->assertFalse(Validation::money('100.1$', 'right')); + $this->assertTrue(Validation::money('100.1$', 'right')); $this->assertFalse(Validation::money('100.1111$', 'right')); $this->assertTrue(Validation::money('€100')); $this->assertTrue(Validation::money('€100.11')); $this->assertTrue(Validation::money('€100.112')); - $this->assertFalse(Validation::money('€100.1')); + $this->assertTrue(Validation::money('€100.1')); $this->assertFalse(Validation::money('€100.1111')); $this->assertTrue(Validation::money('100', 'right')); $this->assertTrue(Validation::money('100.11€', 'right')); $this->assertTrue(Validation::money('100.112€', 'right')); - $this->assertFalse(Validation::money('100.1€', 'right')); + $this->assertTrue(Validation::money('100.1€', 'right')); $this->assertFalse(Validation::money('100.1111€', 'right')); } diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 735f7d94a..e83d45fdf 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -528,7 +528,7 @@ class Validation { * @return boolean Success */ public static function money($check, $symbolPosition = 'left') { - $money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{1})?'; + $money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{1,2})?'; if ($symbolPosition === 'right') { $regex = '/^' . $money . '(? Date: Mon, 29 Apr 2013 00:20:35 +0300 Subject: [PATCH 3/3] removed duplicate test case ```$this->assertFalse(Validation::money('text'));``` is now tested only once --- lib/Cake/Test/Case/Utility/ValidationTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index ee0de0b18..9b1ea2c84 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1965,7 +1965,6 @@ class ValidationTest extends CakeTestCase { $this->assertTrue(Validation::money('100.111,1')); $this->assertTrue(Validation::money('100.111,11')); $this->assertFalse(Validation::money('100.111,111')); - $this->assertFalse(Validation::money('text')); $this->assertTrue(Validation::money('$100')); $this->assertTrue(Validation::money('$100.11'));