From f14e6bae2c353468f1e9f0dfb3e2af64b3061402 Mon Sep 17 00:00:00 2001 From: John Milmine Date: Wed, 5 Jul 2017 07:35:13 +1200 Subject: [PATCH] fix numeric field for null values --- src/Forms/NumericField.php | 5 ++++- tests/php/Forms/NumericFieldTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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..bbe40fdb5 100644 --- a/tests/php/Forms/NumericFieldTest.php +++ b/tests/php/Forms/NumericFieldTest.php @@ -127,6 +127,19 @@ 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 [