浏览代码

Get month() working with the new widget class.

Mark Story 12 年之前
父节点
当前提交
96560ea443
共有 2 个文件被更改,包括 39 次插入37 次删除
  1. 26 25
      src/View/Helper/FormHelper.php
  2. 13 12
      tests/TestCase/View/Helper/FormHelperTest.php

+ 26 - 25
src/View/Helper/FormHelper.php

@@ -1865,12 +1865,16 @@ class FormHelper extends Helper {
 		);
 		$options = $off + $options;
 
+		if (isset($options['value'])) {
+			$options['val'] = $options['value'];
+		}
+
 		// If value is an integer reformat it.
-		if (isset($options['value']) && $options['value'] > 0 && $options['value'] < 31) {
-			$options['value'] = [
+		if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 31) {
+			$options['val'] = [
 				'year' => date('Y'),
 				'month' => date('m'),
-				'day' => (int)$options['value']
+				'day' => (int)$options['val']
 			];
 		}
 		return $this->datetime($fieldName, $options);
@@ -1938,7 +1942,7 @@ class FormHelper extends Helper {
 /**
  * Returns a SELECT element for months.
  *
- * ### Attributes:
+ * ### Options:
  *
  * - `monthNames` - If false, 2 digit numbers will be used instead of text.
  *   If a array, the given array will be used.
@@ -1947,33 +1951,30 @@ class FormHelper extends Helper {
  * - `value` The selected value of the input.
  *
  * @param string $fieldName Prefix name for the SELECT element
- * @param array $attributes Attributes for the select element
+ * @param array $options Attributes for the select element
  * @return string A generated month select dropdown.
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
  */
-	public function month($fieldName, $attributes = array()) {
-		$attributes += array('empty' => true, 'value' => null);
-		$attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
+	public function month($fieldName, $options = array()) {
+		$off = array_diff($this->_datetimeParts, ['month']);
+		$off = array_combine(
+			$off,
+			array_fill(0, count($off), false)
+		);
+		$options = $off + $options;
 
-		if (strlen($attributes['value']) > 2) {
-			$date = date_create($attributes['value']);
-			$attributes['value'] = null;
-			if ($date) {
-				$attributes['value'] = $date->format('m');
-			}
-		} elseif ($attributes['value'] === false) {
-			$attributes['value'] = null;
+		if (isset($options['value'])) {
+			$options['val'] = $options['value'];
 		}
-		$defaults = array('monthNames' => true);
-		$attributes = array_merge($defaults, (array)$attributes);
-		$monthNames = $attributes['monthNames'];
-		unset($attributes['monthNames']);
 
-		return $this->select(
-			$fieldName . ".month",
-			$this->_generateOptions('month', array('monthNames' => $monthNames)),
-			$attributes
-		);
+		if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 12) {
+			$options['val'] = [
+				'year' => date('Y'),
+				'month' => (int)$options['val'],
+				'day' => date('d')
+			];
+		}
+		return $this->datetime($fieldName, $options);
 	}
 
 /**

+ 13 - 12
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5423,10 +5423,10 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testMonth() {
-		$result = $this->Form->month('Model.field');
+		$result = $this->Form->month('Model.field', ['value' => '']);
 		$expected = array(
 			array('select' => array('name' => 'Model[field][month]')),
-			array('option' => array('value' => '')),
+			array('option' => array('value' => '', 'selected' => 'selected')),
 			'/option',
 			array('option' => array('value' => '01')),
 			date('F', strtotime('2008-01-01 00:00:00')),
@@ -5438,10 +5438,10 @@ class FormHelperTest extends TestCase {
 		);
 		$this->assertTags($result, $expected);
 
-		$result = $this->Form->month('Model.field', array('empty' => true));
+		$result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
 		$expected = array(
 			array('select' => array('name' => 'Model[field][month]')),
-			array('option' => array('value' => '')),
+			array('option' => array('selected' => 'selected',  'value' => '')),
 			'/option',
 			array('option' => array('value' => '01')),
 			date('F', strtotime('2008-01-01 00:00:00')),
@@ -5453,30 +5453,31 @@ class FormHelperTest extends TestCase {
 		);
 		$this->assertTags($result, $expected);
 
-		$result = $this->Form->month('Model.field', array('monthNames' => false));
+		$result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
 		$expected = array(
 			array('select' => array('name' => 'Model[field][month]')),
-			array('option' => array('value' => '')),
+			array('option' => array('selected' => 'selected',  'value' => '')),
 			'/option',
 			array('option' => array('value' => '01')),
-			'01',
+			'1',
 			'/option',
 			array('option' => array('value' => '02')),
-			'02',
+			'2',
 			'/option',
 			'*/select',
 		);
 		$this->assertTags($result, $expected);
 
-		$monthNames = array(
+		$monthNames = [
 			'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
-			'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
-		$result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
+			'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
+		];
+		$result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
 		$expected = array(
 			array('select' => array('name' => 'Model[field][month]')),
 			array('option' => array('value' => '')),
 			'/option',
-			array('option' => array('value' => '01')),
+			array('option' => array('value' => '01', 'selected' => 'selected')),
 			'Jan',
 			'/option',
 			array('option' => array('value' => '02')),