Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Applying patch from 'thatcode' to fix an issue where FormHelper::date…

…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...
commit 1371cefc3d0b2993b658b218f188e9de69095cbe 1 parent 29ddffa
@markstory markstory authored
View
9 cake/libs/view/helpers/form.php
@@ -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.
@@ -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) {
View
19 cake/tests/cases/libs/view/helpers/form.test.php
@@ -4237,6 +4237,25 @@ function testDateTime() {
}
/**
+ * 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
*
* test multiple datetime element generation
Please sign in to comment.
Something went wrong with that request. Please try again.