Skip to content

Commit

Permalink
Don't select year 0 when there are all 0's.
Browse files Browse the repository at this point in the history
Year 0 is almost never a 'good' selection value and causes odd behavior
when paired with MySQL.

Fixes #2658
  • Loading branch information
markstory committed Jan 15, 2014
1 parent ace586e commit f25e84f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -6273,6 +6273,25 @@ public function testDateTimeWithBogusData() {
$this->assertNotRegExp('/selected="selected">\d/', $result);
}

/**
* testDateTime all zeros
*
* @return void
*/
public function testDateTimeAllZeros() {
$result = $this->Form->dateTime('Contact.date',
'DMY',
false,
array(
'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
'value' => '0000-00-00'
)
);

$this->assertRegExp('/<option value="">-<\/option>/', $result);
$this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
}

/**
* testDateTimeEmptyAsArray
*
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2825,7 +2825,11 @@ protected function _generateOptions($name, $options = array()) {
if ($min > $max) {
list($min, $max) = array($max, $min);
}
if (!empty($options['value']) && (int)$options['value'] < $min) {
if (
!empty($options['value']) &&
(int)$options['value'] < $min &&
(int)$options['value'] > 0
) {
$min = (int)$options['value'];
} elseif (!empty($options['value']) && (int)$options['value'] > $max) {
$max = (int)$options['value'];
Expand Down

0 comments on commit f25e84f

Please sign in to comment.