Merge pull request #7103 from dnadesign/fix-numericfield-null

This commit is contained in:
Daniel Hensby 2017-07-05 11:45:29 +01:00
commit 2c5e237a93
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 18 additions and 1 deletions

View File

@ -132,7 +132,7 @@ class NumericField extends TextField
public function Value()
{
// Show invalid value back to user in case of error
if ($this->value === false) {
if ($this->value === null || $this->value === false) {
return $this->originalValue;
}
$formatter = $this->getFormatter();
@ -154,6 +154,9 @@ class NumericField extends TextField
*/
protected function cast($value)
{
if (strlen($value) === 0) {
return null;
}
if ($this->getScale() === 0) {
return (int)$value;
}

View File

@ -127,6 +127,20 @@ class NumericFieldTest extends SapphireTest
$this->assertContains('type="text"', $html, 'number type not set');
}
public function testNullSet()
{
$field = new NumericField('Number');
$field->setValue('');
$this->assertEquals('', $field->Value());
$this->assertNull($field->dataValue());
$field->setValue(null);
$this->assertNull($field->Value());
$this->assertNull($field->dataValue());
$field->setValue(0);
$this->assertEquals(0, $field->Value());
$this->assertEquals(0, $field->dataValue());
}
public function dataForTestSubmittedValue()
{
return [