diff --git a/forms/CurrencyField.php b/forms/CurrencyField.php index aa3d7bfca..7a260b0e2 100755 --- a/forms/CurrencyField.php +++ b/forms/CurrencyField.php @@ -13,14 +13,14 @@ class CurrencyField extends TextField { */ function setValue($val) { $value = ($val) ? $val : 0.00; - $this->value = '$' . number_format(ereg_replace('[^0-9.]', '', $value), 2); + $this->value = '$' . number_format((double)preg_replace('/[^0-9.\-]/', '', $value), 2); } /** * Overwrite the datavalue before saving to the db ;-) */ function dataValue() { if($this->value){ - return preg_replace('/[^0-9.]/',"", $this->value); + return preg_replace('/[^0-9.\-]/','', $this->value); }else{ return 0.00; } @@ -54,7 +54,7 @@ Behaviour.register({ if(!el || !el.value) return true; var value = \$F(el); - if(value.length > 0 && !value.match(/^\s*\\\\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*\$/)) { + if(value.length > 0 && !value.match(/^\s*(-?\\\$?|\\\$-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*\$/)) { validationError(el,"$error","validation",false); return false; } @@ -72,7 +72,7 @@ JS; } function validate($validator) { - if(!empty ($this->value) && !preg_match('/^\s*\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/', $this->value)) { + if(!empty ($this->value) && !preg_match('/^\s*(\-?\$?|\$\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/', $this->value)) { $validator->validationError($this->name, _t('Form.VALIDCURRENCY', "Please enter a valid currency."), "validation", false); return false; } diff --git a/tests/forms/CurrencyFieldTest.php b/tests/forms/CurrencyFieldTest.php new file mode 100644 index 000000000..2a4f29a42 --- /dev/null +++ b/tests/forms/CurrencyFieldTest.php @@ -0,0 +1,106 @@ +setValue('123.45'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates positive decimals' + ); + + $f->setValue('-123.45'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates negative decimals' + ); + + $f->setValue('$123.45'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates positive decimals with sign' + ); + + $f->setValue('-$123.45'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates negative decimals with sign' + ); + + $f->setValue('$-123.45'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates negative decimals with sign' + ); + + $f->setValue('324511434634'); + $this->assertTrue( + $f->validate(new RequiredFields()), + 'Validates large integers' + ); + } + + function testSetValue() { + $f = new CurrencyField('TestField'); + + $f->setValue('123.45'); + $this->assertEquals( + $f->value, '$123.45', + 'Prepends dollar sign to positive decimal' + ); + + $f->setValue('-123.45'); + $this->assertEquals( + $f->value, '$-123.45', + 'Prepends dollar sign to negative decimal' + ); + + $f->setValue('$1'); + $this->assertEquals( + $f->value, '$1.00', + 'Formats small value' + ); + + $f->setValue('$2.5'); + $this->assertEquals( + $f->value, '$2.50', + 'Formats small value' + ); + + $f->setValue('$2500000.13'); + $this->assertEquals( + $f->value, '$2,500,000.13', + 'Formats large value' + ); + + $f->setValue('$2.50000013'); + $this->assertEquals( + $f->value, '$2.50', + 'Truncates long decimal portions' + ); + } + + function testDataValue() { + $f = new CurrencyField('TestField'); + + $f->setValue('$123.45'); + $this->assertEquals( + $f->dataValue(), 123.45 + ); + + $f->setValue('-$123.45'); + $this->assertEquals( + $f->dataValue(), -123.45 + ); + + $f->setValue('$-123.45'); + $this->assertEquals( + $f->dataValue(), -123.45 + ); + } +}