Skip to content

Commit

Permalink
Fix incorrectly generated date inputs & selected values.
Browse files Browse the repository at this point in the history
* Generate type = date properly.
* Select the correct input when maxYear and no value are used
  together.
  • Loading branch information
markstory committed Feb 27, 2014
1 parent 2d9b6cc commit 149af37
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 68 deletions.
10 changes: 9 additions & 1 deletion src/View/Helper/FormHelper.php
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
86 changes: 19 additions & 67 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 149af37

Please sign in to comment.