API CHANGE TimeField doesn't internally store value as "Y-m-d g:ia" (including date), but only as "g:ia"

BUGFIX Allowing TimeField and subclasses to be set to NULL through setValue()
BUGFIX Added TimeField->dataValue() to apply same conversion as setValue()
MINOR Added TimeFieldTest

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@79355 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-06-16 06:39:57 +00:00
parent 5fb45eb846
commit efb6f0ceaf
2 changed files with 47 additions and 28 deletions

View File

@ -1,39 +1,38 @@
<?php
/**
* Time field.
* Default Value represented in the format passed as constructor.
*
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
*
* @package forms
* @subpackage fields-datetime
*/
class TimeField extends TextField {
// Stores our time format;
protected $timeformat;
/**
* @var string $timeformat Time description compatible with date() syntax.
*/
protected $timeformat = "g:ia";
/**
* Constructor saves the format difference. Timefields shouldn't
* have a problem with length as times can only be represented in on way.
*
* @param $name string The name of the field
* @param $title string The Title of the field
* @param $value string the value for the field
* @param $timeformat string The Time format in date php format e.g. G:ia
*/
function __construct($name, $title = null, $value = "",$timeformat = "g:ia"){
function __construct($name, $title = null, $value = "",$timeformat = null){
parent::__construct($name,$title,$value);
$this->timeformat = $timeformat;
if($timeformat) $this->timeformat = $timeformat;
}
/**
* Change the setValue to store the time (in a datetime field)
* we store the current date as well (although we don't use it for this field)
*/
function setValue( $val ) {
if( $val )
$this->value = (date("Y-m-d",time()) . " " . date("H:i",strtotime($val)) );
else
$this->value = null;
function dataValue() {
return date($this->timeformat,strtotime($this->value));
}
function setValue($val) {
$this->value = date($this->timeformat,strtotime($val));
}
/**
@ -43,18 +42,6 @@ class TimeField extends TextField {
return new TimeField_Readonly( $this->name, $this->title, $this->dataValue(),$this->timeformat);
}
/**
* Added to the value of the input, put the date into the format
* specified in the constructer.
*/
function attrValue(){
if($this->value){
return date($this->timeformat,strtotime($this->value));
}else{
return "";
}
}
}
/**

View File

@ -0,0 +1,32 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class TimeFieldTest extends SapphireTest {
function testDataValue12h() {
$dateField12h = new TimeField('Time', 'Time');
$dateField12h->setValue('11pm');
$this->assertEquals($dateField12h->dataValue(), '11:00pm');
$dateField12h->setValue('23:59');
$this->assertEquals($dateField12h->dataValue(), '11:59pm');
$dateField12h->setValue('11:59pm');
$this->assertEquals($dateField12h->dataValue(), '11:59pm');
}
function testDataValue24h() {
$dateField24h = new TimeField('Time', 'Time', null, 'H:i');
$dateField24h->setValue('11pm');
$this->assertEquals($dateField24h->dataValue(), '23:00');
$dateField24h->setValue('23:59');
$this->assertEquals($dateField24h->dataValue(), '23:59');
$dateField24h->setValue('11:59pm');
$this->assertEquals($dateField24h->dataValue(), '23:59');
}
}