Merge pull request #1233 from spiliot/patch-1

Money validation should accept one decimal place.
This commit is contained in:
Mark Story 2013-04-28 18:06:49 -07:00
commit 619f66178b
2 changed files with 13 additions and 5 deletions

View file

@ -1958,29 +1958,37 @@ 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->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'));
}

View file

@ -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,2})?';
if ($symbolPosition === 'right') {
$regex = '/^' . $money . '(?<!\x{00a2})\p{Sc}?$/u';
} else {