Open Taitava opened 4 years ago
I can provide a PR for this. I'd just like to confirm, are the two solutions mentioned at the end of my earlier comment okay? In addition to actually fix the setScale()
method, I'd like to add the $scale
parameter to the __construct()
method.
Affected Version 4.5.1
Description
When creating a new
NumericField
in a customForm
and setting a float value to the field, the decimals will get stripped off even if theNumericField::setScale()
method is called right after creating the field (before rendering it). This is because the field's construction process casts the value as an integer.Steps to Reproduce
The scale is 0 during the construction. The field constructor calls
NumericField::setValue()
, which performs casting: https://github.com/silverstripe/silverstripe-framework/blob/3ad4b93daacf3dc3d76a3da6c0287827604a631a/src/Forms/NumericField.php#L142-L147 (Note$this->originalValue
, I'll come to that soon.)NumericField::setScale()
makes the field float/integer, but does not correct the decimals of the current value: https://github.com/silverstripe/silverstripe-framework/blob/3ad4b93daacf3dc3d76a3da6c0287827604a631a/src/Forms/NumericField.php#L307-L318So I suggest that we:
$this->setValue($this->originalValue)
toNumericField::setScale()
after the$this->scale = $scale;
line.setScale(*anything greater than 0*)
.setScale(0)
.A secondary solution would be to add a
$scale
parameter toNumericField::__construct()
and set the scale before setting the value. Or maybe do both of these?