mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR fixed validation on CurrencyField. Will no longer parse non-numeric characters out of input. Added test coverage.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@97057 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
2996cb39e3
commit
6f9177ef43
@ -9,17 +9,19 @@
|
|||||||
*/
|
*/
|
||||||
class CurrencyField extends TextField {
|
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) {
|
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 ;-)
|
* Overwrite the datavalue before saving to the db ;-)
|
||||||
|
* return 0.00 if no value, or value is non-numeric
|
||||||
*/
|
*/
|
||||||
function dataValue() {
|
function dataValue() {
|
||||||
if($this->value){
|
if($this->value && is_numeric($this->value)){
|
||||||
return preg_replace('/[^0-9.]/',"", $this->value);
|
return $this->value;
|
||||||
}else{
|
}else{
|
||||||
return 0.00;
|
return 0.00;
|
||||||
}
|
}
|
||||||
|
25
tests/forms/CurrencyFieldTest.php
Normal file
25
tests/forms/CurrencyFieldTest.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class CurrencyFieldTest extends SapphireTest {
|
||||||
|
function testValidation() {
|
||||||
|
$field = new CurrencyField('cf');
|
||||||
|
$vr = new RequiredFields();
|
||||||
|
|
||||||
|
$field->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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
x
Reference in New Issue
Block a user