BUG Fix decimal scaffolding

Fixes #7454
This commit is contained in:
Damian Mooyman 2017-10-10 10:13:26 +13:00
parent b38942b3fa
commit fd630a99b0
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
2 changed files with 37 additions and 3 deletions

View File

@ -11,7 +11,26 @@ use SilverStripe\Forms\NumericField;
class DBDecimal extends DBField
{
protected $wholeSize, $decimalSize, $defaultValue;
/**
* Whole number size
*
* @var int
*/
protected $wholeSize = 9;
/**
* Decimal scale
*
* @var int
*/
protected $decimalSize = 2;
/**
* Default value
*
* @var string
*/
protected $defaultValue = 0;
/**
* Create a new Decimal field.
@ -27,7 +46,6 @@ class DBDecimal extends DBField
$this->decimalSize = is_int($decimalSize) ? $decimalSize : 2;
$this->defaultValue = number_format((float) $defaultValue, $decimalSize);
;
parent::__construct($name);
}
@ -84,7 +102,8 @@ class DBDecimal extends DBField
*/
public function scaffoldFormField($title = null, $params = null)
{
return new NumericField($this->name, $title);
return NumericField::create($this->name, $title)
->setScale($this->decimalSize);
}
/**

View File

@ -3,12 +3,16 @@
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBDecimal;
class DecimalTest extends SapphireTest
{
protected static $fixture_file = 'DecimalTest.yml';
/**
* @var DecimalTest\TestObject
*/
protected $testDataObject;
protected static $extra_dataobjects = array(
@ -56,4 +60,15 @@ class DecimalTest extends SapphireTest
'Default value for Decimal type is set to 4'
);
}
public function testScaffoldFormField()
{
/** @var DBDecimal $decimal */
$decimal = $this->testDataObject->dbObject('MyDecimal2');
$field = $decimal->scaffoldFormField('The Decimal');
$this->assertEquals(3, $field->getScale());
$field->setValue(1.9999);
$this->assertEquals(1.9999, $field->dataValue());
$this->assertEquals('2.000', $field->Value());
}
}