diff --git a/src/Forms/NumericField.php b/src/Forms/NumericField.php index d4a59efa9..d26d5147e 100644 --- a/src/Forms/NumericField.php +++ b/src/Forms/NumericField.php @@ -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; } diff --git a/tests/php/Forms/NumericFieldTest.php b/tests/php/Forms/NumericFieldTest.php index 73bb2f7be..622c3783b 100644 --- a/tests/php/Forms/NumericFieldTest.php +++ b/tests/php/Forms/NumericFieldTest.php @@ -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 [