Applying patch from 'thatcode' to fix an issue where FormHelper::datetime() would not use 'default' or 'value' keys like other inputs would. This corrects an unintentional inconsistency in the methods. Test cases added. Fixes #988

This commit is contained in:
mark_story 2010-08-09 23:49:18 -04:00
parent 29ddffa2d3
commit 1371cefc3d
2 changed files with 27 additions and 1 deletions

View file

@ -1777,6 +1777,8 @@ class FormHelper extends AppHelper {
* - `separator` The contents of the string between select elements. Defaults to '-'
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
* matching the field name will override this value. If no default is provided `time()` will be used.
*
* @param string $fieldName Prefix name for the SELECT element
* @param string $dateFormat DMY, MDY, YMD.
@ -1792,7 +1794,12 @@ class FormHelper extends AppHelper {
$year = $month = $day = $hour = $min = $meridian = null;
if (empty($selected)) {
$selected = $this->value($fieldName);
$selected = $this->value($attributes, $fieldName);
if (isset($selected['value'])) {
$selected = $selected['value'];
} else {
$selected = null;
}
}
if ($selected === null && $attributes['empty'] != true) {

View file

@ -4236,6 +4236,25 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array('empty' => false));
}
/**
* test that datetime() and default values work.
*
* @return void
*/
function testDatetimeWithDefault() {
$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', null, array('value' => '2009-06-01 11:15:30'));
$this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
$result = $this->Form->dateTime('Contact.updated', 'DMY', '12', null, array(
'default' => '2009-06-01 11:15:30'
));
$this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
}
/**
* testFormDateTimeMulti method
*