Merge pull request #7456 from open-sausages/pulls/4.0/decimal-scaffolding

BUG Fix decimal scaffolding
This commit is contained in:
Daniel Hensby 2017-10-10 15:46:38 +01:00 committed by GitHub
commit c09dec5958
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());
}
}