ソースを参照

Fix selected values in some cases.

When second is missing, or when the meridian is present array values
need to be adjusted.
Mark Story 12 年 前
コミット
6d8062dd9c

+ 7 - 0
src/View/Widget/DateTime.php

@@ -196,6 +196,13 @@ class DateTime implements WidgetInterface {
 				if (isset($value['year'], $value['month'], $value['day'])) {
 					$date->setDate($value['year'], $value['month'], $value['day']);
 				}
+				if (!isset($value['second'])) {
+					$value['second'] = 0;
+				}
+				if (isset($value['meridian'])) {
+					$isAm = strtolower($value['meridian']) === 'am';
+					$value['hour'] = $isAm ? $value['hour'] : $value['hour'] + 12;
+				}
 				if (isset($value['hour'], $value['minute'], $value['second'])) {
 					$date->setTime($value['hour'], $value['minute'], $value['second']);
 				}

+ 13 - 17
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5237,35 +5237,38 @@ class FormHelperTest extends TestCase {
 		extract($this->dateRegex);
 		$now = strtotime('now');
 
-		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
+		$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array(
+			'interval' => 5,
+			'value' => ''
+		));
 		$expected = array(
-			array('select' => array('name' => 'Contact[date][day]')),
-			$daysRegex,
-			array('option' => array('value' => '')),
+			array('select' => array('name' => 'Contact[date][month]')),
+			$monthsRegex,
+			array('option' => array('selected' => 'selected', 'value' => '')),
 			'/option',
 			'*/select',
 
-			array('select' => array('name' => 'Contact[date][month]')),
-			$monthsRegex,
-			array('option' => array('value' => '')),
+			array('select' => array('name' => 'Contact[date][day]')),
+			$daysRegex,
+			array('option' => array('selected' => 'selected', 'value' => '')),
 			'/option',
 			'*/select',
 
 			array('select' => array('name' => 'Contact[date][year]')),
 			$yearsRegex,
-			array('option' => array('value' => '')),
+			array('option' => array('selected' => 'selected', 'value' => '')),
 			'/option',
 			'*/select',
 
 			array('select' => array('name' => 'Contact[date][hour]')),
 			$hoursRegex,
-			array('option' => array('value' => '')),
+			array('option' => array('selected' => 'selected', 'value' => '')),
 			'/option',
 			'*/select',
 
 			array('select' => array('name' => 'Contact[date][minute]')),
 			$minutesRegex,
-			array('option' => array('value' => '')),
+			array('option' => array('selected' => 'selected', 'value' => '')),
 			'/option',
 			array('option' => array('value' => '00')),
 			'00',
@@ -5277,15 +5280,8 @@ class FormHelperTest extends TestCase {
 			'10',
 			'/option',
 			'*/select',
-
-			array('select' => array('name' => 'Contact[date][meridian]')),
-			$meridianRegex,
-			array('option' => array('value' => '')),
-			'/option',
-			'*/select'
 		);
 		$this->assertTags($result, $expected);
-		$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
 	}
 
 /**

+ 35 - 0
tests/TestCase/View/Widget/DateTimeTest.php

@@ -109,6 +109,41 @@ class DateTimeTest extends TestCase {
 	}
 
 /**
+ * Test that render() works with an array for val that is missing seconds.
+ *
+ * @return void
+ */
+	public function testRenderSelectedNoSeconds() {
+		$selected = [
+			'year' => '2014', 'month' => '01', 'day' => '20',
+			'hour' => '12', 'minute' => '30'
+		];
+		$result = $this->DateTime->render(['val' => $selected]);
+		$this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
+		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
+		$this->assertContains('<option value="20" selected="selected">20</option>', $result);
+		$this->assertContains('<option value="12" selected="selected">12</option>', $result);
+		$this->assertContains('<option value="30" selected="selected">30</option>', $result);
+	}
+
+/**
+ * Test that render() adjusts hours based on meridian
+ *
+ * @return void
+ */
+	public function testRenderSelectedMeridian() {
+		$selected = [
+			'year' => '2014', 'month' => '01', 'day' => '20',
+			'hour' => '7', 'minute' => '30', 'meridian' => 'pm'
+		];
+		$result = $this->DateTime->render(['val' => $selected]);
+		$this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
+		$this->assertContains('<option value="01" selected="selected">1</option>', $result);
+		$this->assertContains('<option value="20" selected="selected">20</option>', $result);
+		$this->assertContains('<option value="19" selected="selected">19</option>', $result);
+	}
+
+/**
  * Test rendering widgets with empty values.
  *
  * @retun void