Skip to content
This repository
Browse code

Fix issue where the incorrect meridian would be selected.

When combining 12 hour format, interval and afternoon times, the
incorrect meridian would be selected.  Moving the hour math into
FormHelper::hour() makes that method generally more correct and lenient
on its input.

Fixes #3299
  • Loading branch information...
commit 3729ac1f2f83f9c47de3cf08661891add0f0dbac 1 parent 922d986
Mark Story authored October 23, 2012
40  lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -2224,9 +2224,9 @@ public function testInputTime() {
2224 2224
 			'type' => 'time',
2225 2225
 			'selected' => '18:15'
2226 2226
 		));
2227  
-		$this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
2228  
-		$this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
2229  
-		$this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  2227
+		$this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2228
+		$this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2229
+		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
2230 2230
 	}
2231 2231
 
2232 2232
 /**
@@ -2236,6 +2236,15 @@ public function testInputTime() {
2236 2236
  */
2237 2237
 	public function testTimeSelectedWithInterval() {
2238 2238
 		$result = $this->Form->input('Model.start_time', array(
  2239
+			'type' => 'time',
  2240
+			'interval' => 15,
  2241
+			'selected' => '2012-10-23 15:57:00'
  2242
+		));
  2243
+		$this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2244
+		$this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2245
+		$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2246
+
  2247
+		$result = $this->Form->input('Model.start_time', array(
2239 2248
 			'timeFormat' => 24,
2240 2249
 			'type' => 'time',
2241 2250
 			'interval' => 15,
@@ -5621,26 +5630,11 @@ public function testHour() {
5621 5630
 
5622 5631
 		$this->Form->request->data['Model']['field'] = '';
5623 5632
 		$result = $this->Form->hour('Model.field', true, array('value' => '23'));
5624  
-		$expected = array(
5625  
-			array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
5626  
-			array('option' => array('value' => '')),
5627  
-			'/option',
5628  
-			array('option' => array('value' => '00')),
5629  
-			'0',
5630  
-			'/option',
5631  
-			array('option' => array('value' => '01')),
5632  
-			'1',
5633  
-			'/option',
5634  
-			array('option' => array('value' => '02')),
5635  
-			'2',
5636  
-			'/option',
5637  
-			$hoursRegex,
5638  
-			array('option' => array('value' => '23', 'selected' => 'selected')),
5639  
-			'23',
5640  
-			'/option',
5641  
-			'/select',
5642  
-		);
5643  
-		$this->assertTags($result, $expected);
  5633
+		$this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5634
+
  5635
+		$result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5636
+		$this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5637
+
5644 5638
 
5645 5639
 		$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
5646 5640
 		$result = $this->Form->hour('Model.field', true);
10  lib/Cake/View/Helper/FormHelper.php
@@ -2042,6 +2042,11 @@ public function hour($fieldName, $format24Hours = false, $attributes = array())
2042 2042
 		} elseif ($attributes['value'] === false) {
2043 2043
 			$attributes['value'] = null;
2044 2044
 		}
  2045
+
  2046
+		if ($attributes['value'] > 12 && !$format24Hours) {
  2047
+			$attributes['value'] -= 12;
  2048
+		}
  2049
+
2045 2050
 		return $this->select(
2046 2051
 			$fieldName . ".hour",
2047 2052
 			$this->_generateOptions($format24Hours ? 'hour24' : 'hour'),
@@ -2206,10 +2211,7 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
2206 2211
 				if (!empty($timeFormat)) {
2207 2212
 					$time = explode(':', $days[1]);
2208 2213
 
2209  
-					if (($time[0] > 12) && $timeFormat == '12') {
2210  
-						$time[0] = $time[0] - 12;
2211  
-						$meridian = 'pm';
2212  
-					} elseif ($time[0] == '12' && $timeFormat == '12') {
  2214
+					if ($time[0] >= '12' && $timeFormat == '12') {
2213 2215
 						$meridian = 'pm';
2214 2216
 					} elseif ($time[0] == '00' && $timeFormat == '12') {
2215 2217
 						$time[0] = 12;

0 notes on commit 3729ac1

Please sign in to comment.
Something went wrong with that request. Please try again.