BUGFIX Conditionally setting and getting NULL values in TimeField->setValue()/dataValue(). Previously overriding a TimeField with an empty value wasn't possible.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@79357 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-06-16 07:03:17 +00:00
parent efb6f0ceaf
commit 204620b99d
2 changed files with 39 additions and 16 deletions

View File

@ -28,11 +28,20 @@ class TimeField extends TextField {
} }
function dataValue() { 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) { function setValue($val) {
$this->value = date($this->timeformat,strtotime($val)); if($val) {
$this->value = date($this->timeformat,strtotime($val));
} else {
$this->value = $val;
}
} }
/** /**

View File

@ -5,28 +5,42 @@
*/ */
class TimeFieldTest extends SapphireTest { class TimeFieldTest extends SapphireTest {
function testDataValue12h() { function testDataValue12h() {
$dateField12h = new TimeField('Time', 'Time'); $field12h = new TimeField('Time', 'Time');
$dateField12h->setValue('11pm'); $field12h->setValue('11pm');
$this->assertEquals($dateField12h->dataValue(), '11:00pm'); $this->assertEquals($field12h->dataValue(), '11:00pm');
$dateField12h->setValue('23:59'); $field12h->setValue('23:59');
$this->assertEquals($dateField12h->dataValue(), '11:59pm'); $this->assertEquals($field12h->dataValue(), '11:59pm');
$dateField12h->setValue('11:59pm'); $field12h->setValue('11:59pm');
$this->assertEquals($dateField12h->dataValue(), '11:59pm'); $this->assertEquals($field12h->dataValue(), '11:59pm');
$field12h->setValue('11:59 pm');
$this->assertEquals($field12h->dataValue(), '11:59pm');
} }
function testDataValue24h() { function testDataValue24h() {
$dateField24h = new TimeField('Time', 'Time', null, 'H:i'); $field24h = new TimeField('Time', 'Time', null, 'H:i');
$dateField24h->setValue('11pm'); $field24h->setValue('11pm');
$this->assertEquals($dateField24h->dataValue(), '23:00'); $this->assertEquals($field24h->dataValue(), '23:00');
$dateField24h->setValue('23:59'); $field24h->setValue('23:59');
$this->assertEquals($dateField24h->dataValue(), '23:59'); $this->assertEquals($field24h->dataValue(), '23:59');
$dateField24h->setValue('11:59pm'); $field24h->setValue('11:59pm');
$this->assertEquals($dateField24h->dataValue(), '23:59'); $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(), '');
} }
} }