diff --git a/forms/TimeField.php b/forms/TimeField.php index 22e243bee..3ac0eefc0 100755 --- a/forms/TimeField.php +++ b/forms/TimeField.php @@ -28,11 +28,20 @@ class TimeField extends TextField { } function dataValue() { - return date($this->timeformat,strtotime($this->value)); + if($this->value) { + return date($this->timeformat,strtotime($this->value)); + } else { + return $this->value; + } } function setValue($val) { - $this->value = date($this->timeformat,strtotime($val)); + if($val) { + $this->value = date($this->timeformat,strtotime($val)); + } else { + $this->value = $val; + } + } /** diff --git a/tests/forms/TimeFieldTest.php b/tests/forms/TimeFieldTest.php index fb8e9fd98..fe9c69b02 100644 --- a/tests/forms/TimeFieldTest.php +++ b/tests/forms/TimeFieldTest.php @@ -5,28 +5,42 @@ */ class TimeFieldTest extends SapphireTest { function testDataValue12h() { - $dateField12h = new TimeField('Time', 'Time'); + $field12h = new TimeField('Time', 'Time'); - $dateField12h->setValue('11pm'); - $this->assertEquals($dateField12h->dataValue(), '11:00pm'); + $field12h->setValue('11pm'); + $this->assertEquals($field12h->dataValue(), '11:00pm'); - $dateField12h->setValue('23:59'); - $this->assertEquals($dateField12h->dataValue(), '11:59pm'); + $field12h->setValue('23:59'); + $this->assertEquals($field12h->dataValue(), '11:59pm'); - $dateField12h->setValue('11:59pm'); - $this->assertEquals($dateField12h->dataValue(), '11:59pm'); + $field12h->setValue('11:59pm'); + $this->assertEquals($field12h->dataValue(), '11:59pm'); + + $field12h->setValue('11:59 pm'); + $this->assertEquals($field12h->dataValue(), '11:59pm'); } function testDataValue24h() { - $dateField24h = new TimeField('Time', 'Time', null, 'H:i'); + $field24h = new TimeField('Time', 'Time', null, 'H:i'); - $dateField24h->setValue('11pm'); - $this->assertEquals($dateField24h->dataValue(), '23:00'); + $field24h->setValue('11pm'); + $this->assertEquals($field24h->dataValue(), '23:00'); - $dateField24h->setValue('23:59'); - $this->assertEquals($dateField24h->dataValue(), '23:59'); + $field24h->setValue('23:59'); + $this->assertEquals($field24h->dataValue(), '23:59'); - $dateField24h->setValue('11:59pm'); - $this->assertEquals($dateField24h->dataValue(), '23:59'); + $field24h->setValue('11:59pm'); + $this->assertEquals($field24h->dataValue(), '23:59'); + + $field24h->setValue('11:59 pm'); + $this->assertEquals($field24h->dataValue(), '23:59'); + } + + function testOverrideWithNull() { + $field = new TimeField('Time', 'Time'); + + $field->setValue('11:00pm'); + $field->setValue(''); + $this->assertEquals($field->dataValue(), ''); } } \ No newline at end of file