Skip to content

Commit

Permalink
Applying patch from 'thatcode' to fix an issue where FormHelper::date…
Browse files Browse the repository at this point in the history
…time() would not use 'default' or 'value' keys like other inputs would. This corrects an unintentional inconsistency in the methods. Test cases added. Fixes #988
  • Loading branch information
markstory committed Aug 10, 2010
1 parent 29ddffa commit 1371cef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cake/libs/view/helpers/form.php
Expand Up @@ -1777,6 +1777,8 @@ function meridian($fieldName, $selected = null, $attributes = array()) {
* - `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.
Expand All @@ -1792,7 +1794,12 @@ function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected
$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) {
Expand Down
19 changes: 19 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -4236,6 +4236,25 @@ function testDateTime() {
$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
*
Expand Down

0 comments on commit 1371cef

Please sign in to comment.