Skip to content
Browse files

Add date type to FormHelper.

Add a concrete method for generating date inputs and remove workarounds
in _getInput().
  • Loading branch information...
1 parent 2eb552f commit 63b2dd401ec169efc1c6eb6fe3bd83100ea9885b @markstory markstory committed
Showing with 57 additions and 7 deletions.
  1. +39 −7 src/View/Helper/FormHelper.php
  2. +18 −0 tests/TestCase/View/Helper/FormHelperTest.php
View
46 src/View/Helper/FormHelper.php
@@ -887,8 +887,6 @@ protected function _getInput($fieldName, $options) {
case 'url':
$options = $this->_initInputField($fieldName, $options);
return $this->widget($options['type'], $options);
- case 'date':
- return $this->dateTime($fieldName, $options);
default:
return $this->{$options['type']}($fieldName, $options);
}
@@ -1986,14 +1984,10 @@ protected function _datetimeOptions($options) {
* - `timeFormat` The time format to use, either 12 or 24.
* - `second` Set to true to enable seconds drop down.
*
- * To control the order of inputs, and any elements/content between the inputs you
- * can override the `dateWidget` template. By default the `dateWidget` template is:
- *
- * `{{month}}{{day}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}`
- *
* @param string $fieldName Prefix name for the SELECT element
* @param array $options Array of Options
* @return string Generated set of select boxes for time formats chosen.
+ * @see Cake\View\Helper\FormHelper::dateTime() for templating options.
*/
public function time($fieldName, $options = []) {
$options += [
@@ -2012,6 +2006,44 @@ public function time($fieldName, $options = []) {
}
/**
+ * Generate date inputs.
+ *
+ * ### Options:
+ *
+ * ### Options:
+ *
+ * - `monthNames` If false, 2 digit numbers will be used instead of text.
+ * If a array, the given array will be used.
+ * - `minYear` The lowest year to use in the year select
+ * - `maxYear` The maximum year to use in the year select
+ * - `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 array $options Array of Options
+ * @return string Generated set of select boxes for time formats chosen.
+ * @see Cake\View\Helper\FormHelper::dateTime() for templating options.
+ */
+ public function date($fieldName, $options = []) {
+ $options += [
+ 'empty' => true,
+ 'value' => null,
+ 'monthNames' => true,
+ 'minYear' => null,
+ 'maxYear' => null,
+ 'orderYear' => 'desc',
+ ];
+ $options['hour'] = $options['minute'] = false;
+ $options['meridian'] = $options['second'] = false;
+ $options = $this->_initInputField($fieldName, $options);
+ $options = $this->_datetimeOptions($options);
+
+ return $this->widget('datetime', $options);
+ }
+
+/**
* Sets field defaults and adds field to form security input hash.
* Will also add the error class if the field contains validation errors.
*
View
18 tests/TestCase/View/Helper/FormHelperTest.php
@@ -3821,6 +3821,24 @@ public function testTime() {
}
/**
+ * Test the date type.
+ *
+ * @return void
+ */
+ public function testDate() {
+ $result = $this->Form->date('start_day', array(
+ 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
+ ));
+ $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
+ $this->assertContains('<option value="03" selected="selected">March</option>', $result);
+ $this->assertContains('<option value="08" selected="selected">8</option>', $result);
+ $this->assertNotContains('hour', $result);
+ $this->assertNotContains('minute', $result);
+ $this->assertNotContains('second', $result);
+ $this->assertNotContains('meridian', $result);
+ }
+
+/**
* testDateTime method
*
* Test generation of date/time select elements

0 comments on commit 63b2dd4

Please sign in to comment.
Something went wrong with that request. Please try again.