Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix incorrectly generated date inputs & selected values.

* Generate type = date properly.
* Select the correct input when maxYear and no value are used
  together.
  • Loading branch information...
commit 149af3716403710bda737d5b25f18ba1d96a3676 1 parent 2d9b6cc
@markstory markstory authored
View
10 src/View/Helper/FormHelper.php
@@ -894,6 +894,8 @@ 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);
}
@@ -1991,8 +1993,14 @@ protected function _datetimeOptions($options) {
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;
}
View
86 tests/TestCase/View/Helper/FormHelperTest.php
@@ -5406,68 +5406,24 @@ public function testYearAutoExpandRange() {
}
/**
- * 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 @@ public function testInputDate() {
* @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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.