Browse Source

Fix incorrectly generated date inputs & selected values.

* Generate type = date properly.
* Select the correct input when maxYear and no value are used
  together.
Mark Story 12 years ago
parent
commit
149af37164
2 changed files with 28 additions and 68 deletions
  1. 9 1
      src/View/Helper/FormHelper.php
  2. 19 67
      tests/TestCase/View/Helper/FormHelperTest.php

+ 9 - 1
src/View/Helper/FormHelper.php

@@ -894,6 +894,8 @@ class FormHelper extends Helper {
 			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);
 		}
@@ -1991,8 +1993,14 @@ class FormHelper extends Helper {
 		unset($options['interval'], $options['round']);
 
 		if (!isset($options['val'])) {
-			$options['val'] = new \DateTime();
+			$val = new \DateTime();
+			$currentYear = $val->format('Y');
+			if (isset($options['year']['end']) && $options['year']['end'] < $currentYear) {
+				$val->setDate($options['year']['end'], $val->format('n'), $val->format('j'));
+			}
+			$options['val'] = $val;
 		}
+
 		return $options;
 	}
 

+ 19 - 67
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5406,68 +5406,24 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
- * testInputDate method
- *
- * Test various inputs with type date and different dateFormat values.
- * Failing to provide a dateFormat key should not error.
- * It should simply not pre-select any value then.
+ * Test that input() accepts the type of date and passes options in.
  *
  * @return void
  */
 	public function testInputDate() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
 		$this->Form->request->data = array(
-			'User' => array(
-				'month_year' => array('month' => date('m')),
-				'just_year' => array('month' => date('m')),
-				'just_month' => array('year' => date('Y')),
-				'just_day' => array('month' => date('m')),
-			)
-		);
-		$this->Form->create('User');
-		$result = $this->Form->input('month_year',
-				array(
-					'label' => false,
-					'div' => false,
-					'type' => 'date',
-					'dateFormat' => 'MY',
-					'minYear' => 2006,
-					'maxYear' => 2008
-				)
-		);
-		$this->assertContains('value="' . date('m') . '" selected="selected"', $result);
-		$this->assertNotContains('value="2008" selected="selected"', $result);
-
-		$result = $this->Form->input('just_year',
-			array(
-				'type' => 'date',
-				'label' => false,
-				'dateFormat' => 'Y',
-				'minYear' => date('Y'),
-				'maxYear' => date('Y', strtotime('+20 years'))
-			)
+			'month_year' => array('month' => date('m')),
 		);
-		$this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
-
-		$result = $this->Form->input('just_month',
-			array(
-				'type' => 'date',
+		$this->Form->create($this->article);
+		$result = $this->Form->input('month_year', array(
 				'label' => false,
-				'dateFormat' => 'M',
-				'empty' => false,
-			)
-		);
-		$this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
-
-		$result = $this->Form->input('just_day',
-			array(
+				'div' => false,
 				'type' => 'date',
-				'label' => false,
-				'dateFormat' => 'D',
-				'empty' => false,
-			)
-		);
-		$this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
+				'minYear' => 2006,
+				'maxYear' => 2008
+		));
+		$this->assertContains('value="' . date('m') . '" selected="selected"', $result);
+		$this->assertNotContains('value="2008" selected="selected"', $result);
 	}
 
 /**
@@ -5479,19 +5435,15 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testInputDateMaxYear() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
-		$this->Form->request->data = array();
-		$this->Form->create('User');
-		$result = $this->Form->input('birthday',
-				array(
-					'label' => false,
-					'div' => false,
-					'type' => 'date',
-					'dateFormat' => 'DMY',
-					'minYear' => 2006,
-					'maxYear' => 2008
-				)
-		);
+		$this->Form->request->data = [];
+		$this->Form->create($this->article);
+		$result = $this->Form->input('birthday', array(
+			'label' => false,
+			'div' => false,
+			'type' => 'date',
+			'minYear' => 2006,
+			'maxYear' => 2008
+		));
 		$this->assertContains('value="2008" selected="selected"', $result);
 	}