Browse Source

Don't select year 0 when there are all 0's.

Year 0 is almost never a 'good' selection value and causes odd behavior
when paired with MySQL.

Fixes #2658
mark_story 12 years ago
parent
commit
f25e84f4fb

+ 19 - 0
lib/Cake/Test/Case/View/Helper/FormHelperTest.php

@@ -6274,6 +6274,25 @@ class FormHelperTest extends CakeTestCase {
 	}
 
 /**
+ * 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
  *
  * @return void

+ 5 - 1
lib/Cake/View/Helper/FormHelper.php

@@ -2825,7 +2825,11 @@ class FormHelper extends AppHelper {
 				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'];