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) (from r108422)
BUGFIX: Take note of output format when building Location header for RestfulServer (from r108427) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112744 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
684e76e36b
commit
43c0e432ed
@ -375,7 +375,16 @@ class RestfulServer extends Controller {
|
||||
|
||||
$this->getResponse()->setStatusCode(200); // Success
|
||||
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
||||
$objHref = Director::absoluteURL(self::$api_base . "$obj->class/$obj->ID");
|
||||
|
||||
// Append the default extension for the output format to the Location header
|
||||
// or else we'll use the default (XML)
|
||||
$types = $responseFormatter->supportedExtensions();
|
||||
$type = '';
|
||||
if (count($types)) {
|
||||
$type = ".{$types[0]}";
|
||||
}
|
||||
|
||||
$objHref = Director::absoluteURL(self::$api_base . "$obj->class/$obj->ID" . $type);
|
||||
$this->getResponse()->addHeader('Location', $objHref);
|
||||
|
||||
return $responseFormatter->convertDataObject($obj);
|
||||
@ -423,7 +432,16 @@ class RestfulServer extends Controller {
|
||||
|
||||
$this->getResponse()->setStatusCode(201); // Created
|
||||
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
||||
$objHref = Director::absoluteURL(self::$api_base . "$obj->class/$obj->ID");
|
||||
|
||||
// Append the default extension for the output format to the Location header
|
||||
// or else we'll use the default (XML)
|
||||
$types = $responseFormatter->supportedExtensions();
|
||||
$type = '';
|
||||
if (count($types)) {
|
||||
$type = ".{$types[0]}";
|
||||
}
|
||||
|
||||
$objHref = Director::absoluteURL(self::$api_base . "$obj->class/$obj->ID" . $type);
|
||||
$this->getResponse()->addHeader('Location', $objHref);
|
||||
|
||||
return $responseFormatter->convertDataObject($obj);
|
||||
|
@ -14,15 +14,15 @@ class CurrencyField extends TextField {
|
||||
*/
|
||||
function setValue($val) {
|
||||
if(!$val) $val = 0.00;
|
||||
$this->value = preg_replace('/^[^\d]/', '', $val);
|
||||
$this->value = '$' . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2);
|
||||
}
|
||||
/**
|
||||
* Overwrite the datavalue before saving to the db ;-)
|
||||
* return 0.00 if no value, or value is non-numeric
|
||||
*/
|
||||
function dataValue() {
|
||||
if($this->value && is_numeric($this->value)){
|
||||
return $this->value;
|
||||
if($this->value) {
|
||||
return preg_replace('/[^0-9.\-]/','', $this->value);
|
||||
}else{
|
||||
return 0.00;
|
||||
}
|
||||
@ -56,7 +56,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;
|
||||
}
|
||||
@ -74,7 +74,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;
|
||||
}
|
||||
|
@ -176,6 +176,7 @@ class RestfulServerTest extends SapphireTest {
|
||||
$this->assertNotEquals($responseArr['ID'], $comment1->ID);
|
||||
$this->assertEquals($responseArr['Comment'], 'created');
|
||||
$this->assertEquals($responseArr['Name'], 'New Comment');
|
||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID']));
|
||||
|
||||
unset($_SERVER['PHP_AUTH_USER']);
|
||||
unset($_SERVER['PHP_AUTH_PW']);
|
||||
@ -201,6 +202,7 @@ class RestfulServerTest extends SapphireTest {
|
||||
$body = '{"Comment":"updated"}';
|
||||
$response = Director::test($url, null, null, 'PUT', $body);
|
||||
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
||||
$obj = Convert::json2obj($response->getBody());
|
||||
$this->assertEquals($obj->ID, $comment1->ID);
|
||||
$this->assertEquals($obj->Comment, 'updated');
|
||||
@ -229,6 +231,7 @@ class RestfulServerTest extends SapphireTest {
|
||||
$body = '<RestfulServerTest_Comment><Comment>updated</Comment></RestfulServerTest_Comment>';
|
||||
$response = Director::test($url, null, null, 'PUT', $body);
|
||||
$this->assertEquals($response->getStatusCode(), 200); // Updated
|
||||
$this->assertEquals($response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url));
|
||||
$obj = Convert::xml2array($response->getBody());
|
||||
$this->assertEquals($obj['ID'], $comment1->ID);
|
||||
$this->assertEquals($obj['Comment'], 'updated');
|
||||
@ -514,4 +517,4 @@ class RestfulServerTest_AuthorRating extends DataObject implements TestOnly {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
?>
|
||||
|
@ -1,25 +1,107 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
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 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 testDataValues() {
|
||||
$field = new CurrencyField('cf');
|
||||
function testSetValue() {
|
||||
$f = new CurrencyField('TestField');
|
||||
|
||||
$field->setValue('$10.34');
|
||||
$this->assertEquals($field->dataValue(), '10.34');
|
||||
$f->setValue('123.45');
|
||||
$this->assertEquals(
|
||||
$f->value, '$123.45',
|
||||
'Prepends dollar sign to positive decimal'
|
||||
);
|
||||
|
||||
$field->setValue('$1s0.34');
|
||||
$this->assertEquals($field->dataValue(), '0.00');
|
||||
$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