Merge pull request #1159 from chillu/pulls/datetimefield-field-setters

DatetimeField->setDateField()/setTimeField()
This commit is contained in:
Ingo Schommer 2013-02-14 11:31:31 -08:00
commit f4068371fc
2 changed files with 98 additions and 10 deletions

View File

@ -216,6 +216,24 @@ class DatetimeField extends FormField {
return $this->dateField;
}
/**
* @param FormField
*/
public function setDateField($field) {
$expected = $this->getName() . '[date]';
if($field->getName() != $expected) {
throw new InvalidArgumentException(sprintf(
'Wrong name format for date field: "%s" (expected "%s")',
$field->getName(),
$expected
));
}
$field->setForm($this->getForm());
$this->dateField = $field;
$this->setValue($this->value); // update value
}
/**
* @return TimeField
*/
@ -223,6 +241,24 @@ class DatetimeField extends FormField {
return $this->timeField;
}
/**
* @param FormField
*/
public function setTimeField($field) {
$expected = $this->getName() . '[time]';
if($field->getName() != $expected) {
throw new InvalidArgumentException(sprintf(
'Wrong name format for time field: "%s" (expected "%s")',
$field->getName(),
$expected
));
}
$field->setForm($this->getForm());
$this->timeField = $field;
$this->setValue($this->value); // update value
}
/**
* @return FormField
*/

View File

@ -25,16 +25,9 @@ class DatetimeFieldTest extends SapphireTest {
}
public function testFormSaveInto() {
$form = new Form(
new Controller(),
'Form',
new FieldList(
$f = new DatetimeField('MyDatetime', null)
),
new FieldList(
new FormAction('doSubmit')
)
);
$f = new DatetimeField('MyDatetime', null);
$form = $this->getMockForm();
$form->Fields()->push($f);
$f->setValue(array(
'date' => '29/03/2003',
'time' => '23:59:38'
@ -170,6 +163,65 @@ class DatetimeFieldTest extends SapphireTest {
date_default_timezone_set($oldTz);
}
public function testSetDateField() {
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setValue(array(
'date' => '24/06/2003',
'time' => '23:59:59',
));
$dateField = new DateField('Datetime[date]');
$field->setDateField($dateField);
$this->assertEquals(
$dateField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'2003-06-24',
$dateField->dataValue(),
'Sets existing value on new field'
);
}
public function testSetTimeField() {
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setValue(array(
'date' => '24/06/2003',
'time' => '23:59:59',
));
$timeField = new TimeField('Datetime[time]');
$field->setTimeField($timeField);
$this->assertEquals(
$timeField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'23:59:59',
$timeField->dataValue(),
'Sets existing value on new field'
);
}
protected function getMockForm() {
return new Form(
new Controller(),
'Form',
new FieldList(),
new FieldList(
new FormAction('doSubmit')
)
);
}
}
/**