Browse Source

Update meridian() to use datetime widget class.

Mark Story 12 years ago
parent
commit
decbef759c
2 changed files with 32 additions and 28 deletions
  1. 9 28
      src/View/Helper/FormHelper.php
  2. 23 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 9 - 28
src/View/Helper/FormHelper.php

@@ -2048,39 +2048,20 @@ class FormHelper extends Helper {
  * - `value` The selected value of the input.
  *
  * @param string $fieldName Prefix name for the SELECT element
- * @param array $attributes Array of Attributes
+ * @param array $options Array of options
  * @return string Completed meridian select input
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::meridian
  */
-	public function meridian($fieldName, $attributes = array()) {
-		$attributes += array('empty' => true, 'value' => null);
-		if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
-			if (is_array($value)) {
-				$meridian = null;
-				extract($value);
-				$attributes['value'] = $meridian;
-			} else {
-				if (empty($value)) {
-					if (!$attributes['empty']) {
-						$attributes['value'] = date('a');
-					}
-				} else {
-					$date = date_create($attributes['value']);
-					$attributes['value'] = null;
-					if ($date) {
-						$attributes['value'] = $date->format('a');
-					}
-				}
-			}
-		}
+	public function meridian($fieldName, $options = array()) {
+		$options = $this->_singleDatetime($options, 'meridian');
 
-		if ($attributes['value'] === false) {
-			$attributes['value'] = null;
+		if (isset($options['val'])) {
+			$options['val'] = [
+				'hour' => date('H'),
+				'minute' => (int)$options['val'],
+			];
 		}
-		return $this->select(
-			$fieldName . ".meridian", $this->_generateOptions('meridian'),
-			$attributes
-		);
+		return $this->datetime($fieldName, $options);
 	}
 
 /**

+ 23 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5687,6 +5687,29 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test generating an input for the meridian.
+ *
+ * @return void
+ */
+	public function testMeridian() {
+		extract($this->dateRegex);
+
+		$now = time();
+		$result = $this->Form->meridian('Model.field', ['value' => 'am']);
+		$expected = [
+			array('select' => array('name' => 'Model[field][meridian]')),
+			array('option' => array('value' => '')),
+			'/option',
+			$meridianRegex,
+			array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
+			date('a', $now),
+			'/option',
+			'*/select'
+		];
+		$this->assertTags($result, $expected);
+	}
+
+/**
  * testHour method
  *
  * @return void