diff --git a/forms/CurrencyField.php b/forms/CurrencyField.php index 90d9ad328..d921168a1 100755 --- a/forms/CurrencyField.php +++ b/forms/CurrencyField.php @@ -9,17 +9,19 @@ */ class CurrencyField extends TextField { /** - * allows the value to be set ( not including $ signs and number format...) + * allows the value to be set. removes the first character + * if it is not a number (probably a currency symbol) */ function setValue($val) { - $this->value = '$' . number_format(ereg_replace('[^0-9.]',"",$val), 2); + $this->value = preg_replace('/^[^\d]/', '', $val); } /** * Overwrite the datavalue before saving to the db ;-) + * return 0.00 if no value, or value is non-numeric */ function dataValue() { - if($this->value){ - return preg_replace('/[^0-9.]/',"", $this->value); + if($this->value && is_numeric($this->value)){ + return $this->value; }else{ return 0.00; } diff --git a/tests/forms/CurrencyFieldTest.php b/tests/forms/CurrencyFieldTest.php new file mode 100644 index 000000000..39bc668e3 --- /dev/null +++ b/tests/forms/CurrencyFieldTest.php @@ -0,0 +1,25 @@ +setValue('$10.23'); + $this->assertTrue($field->validate($vr)); + + $field->setValue('$1a0.23'); + $this->assertFalse($field->validate($vr)); + } + + function testDataValues() { + $field = new CurrencyField('cf'); + + $field->setValue('$10.34'); + $this->assertEquals($field->dataValue(), '10.34'); + + $field->setValue('$1s0.34'); + $this->assertEquals($field->dataValue(), '0.00'); + } +} +?> \ No newline at end of file