diff --git a/src/ORM/FieldType/DBDecimal.php b/src/ORM/FieldType/DBDecimal.php index 437d2908e..ec4a4026c 100644 --- a/src/ORM/FieldType/DBDecimal.php +++ b/src/ORM/FieldType/DBDecimal.php @@ -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); } /** diff --git a/tests/php/ORM/DecimalTest.php b/tests/php/ORM/DecimalTest.php index 6e07f9898..aa6d99b9c 100644 --- a/tests/php/ORM/DecimalTest.php +++ b/tests/php/ORM/DecimalTest.php @@ -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()); + } }