mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX CurrencyField doesn't accept negative value (#5769, thanks simon_w)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@108422 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
b4101984e4
commit
d07888b9ab
@ -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;
|
||||
}
|
||||
|
106
tests/forms/CurrencyFieldTest.php
Normal file
106
tests/forms/CurrencyFieldTest.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class CurrencyFieldTest extends SapphireTest {
|
||||
|
||||
function testValidate() {
|
||||
$f = new CurrencyField('TestField');
|
||||
|
||||
$f->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
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user