Permalink
Browse files

Update hour() picker to use datetime().

  • Loading branch information...
1 parent 5114160 commit 47a8ac483f40676b085a548b7cac1c0e979c52b2 @markstory markstory committed Feb 23, 2014
Showing with 25 additions and 43 deletions.
  1. +12 −29 src/View/Helper/FormHelper.php
  2. +13 −14 tests/TestCase/View/Helper/FormHelperTest.php
@@ -1961,44 +1961,27 @@ public function month($fieldName, $options = array()) {
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
* - `value` The selected value of the input.
+ * - `format` Set to 12 or 24 to use 12 or 24 hour formatting. Defaults to 12.
*
* @param string $fieldName Prefix name for the SELECT element
- * @param boolean $format24Hours True for 24 hours format
* @param array $attributes List of HTML attributes
* @return string Completed hour select input
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hour
*/
- public function hour($fieldName, $format24Hours = false, $attributes = array()) {
- $attributes += array('empty' => true, 'value' => null);
- $attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
+ public function hour($fieldName, $options = []) {
+ $options += ['format' => 12];
+ $options = $this->_singleDatetime($options, 'hour');
- if (strlen($attributes['value']) > 2) {
- try {
- $date = new DateTime($attributes['value']);
- if ($format24Hours) {
- $attributes['value'] = $date->format('H');
- } else {
- $attributes['value'] = $date->format('g');
- }
- } catch (Exception $e) {
- $attributes['value'] = null;
- }
- } elseif ($attributes['value'] === false) {
- $attributes['value'] = null;
- }
+ $options['timeFormat'] = $options['format'];
+ unset($options['format']);
- if ($attributes['value'] > 12 && !$format24Hours) {
- $attributes['value'] -= 12;
- }
- if (($attributes['value'] === 0 || $attributes['value'] === '00') && !$format24Hours) {
- $attributes['value'] = 12;
+ if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 24) {
+ $options['val'] = [
+ 'hour' => (int)$options['val'],
+ 'minute' => date('i'),
+ ];
}
-
- return $this->select(
- $fieldName . ".hour",
- $this->_generateOptions($format24Hours ? 'hour24' : 'hour'),
- $attributes
- );
+ return $this->datetime($fieldName, $options);
}
/**
@@ -1946,7 +1946,7 @@ public function testFormSecuredAndDisabled() {
$this->Form->year('Model.year', array('disabled' => true));
$this->Form->month('Model.month', array('disabled' => true));
$this->Form->day('Model.day', array('disabled' => true));
- $this->Form->hour('Model.hour', false, array('disabled' => true));
+ $this->Form->hour('Model.hour', array('disabled' => true));
$this->Form->minute('Model.minute', array('disabled' => true));
$this->Form->meridian('Model.meridian', array('disabled' => true));
@@ -5693,13 +5693,12 @@ public function testMinute() {
* @return void
*/
public function testHour() {
- $this->markTestIncomplete('Need to revisit once models work again.');
extract($this->dateRegex);
- $result = $this->Form->hour('Model.field', false);
+ $result = $this->Form->hour('Model.field', ['value' => '']);
$expected = array(
- array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
- array('option' => array('value' => '')),
+ array('select' => array('name' => 'Model[field][hour]')),
+ array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
@@ -5713,9 +5712,9 @@ public function testHour() {
$this->assertTags($result, $expected);
$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
- $result = $this->Form->hour('Model.field', false);
+ $result = $this->Form->hour('Model.field', ['format' => 12]);
$expected = array(
- array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
+ array('select' => array('name' => 'Model[field][hour]')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
@@ -5733,16 +5732,16 @@ public function testHour() {
$this->assertTags($result, $expected);
$this->Form->request->data['Model']['field'] = '';
- $result = $this->Form->hour('Model.field', true, array('value' => '23'));
+ $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
$this->assertContains('<option value="23" selected="selected">23</option>', $result);
- $result = $this->Form->hour('Model.field', false, array('value' => '23'));
+ $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
$this->assertContains('<option value="11" selected="selected">11</option>', $result);
$this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
- $result = $this->Form->hour('Model.field', true);
+ $result = $this->Form->hour('Model.field', ['format' => 24]);
$expected = array(
- array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
+ array('select' => array('name' => 'Model[field][hour]')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '00', 'selected' => 'selected')),
@@ -5760,15 +5759,15 @@ public function testHour() {
$this->assertTags($result, $expected);
unset($this->Form->request->data['Model']['field']);
- $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
+ $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
$thisHour = date('H');
$optValue = date('G');
$this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
$this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
- $result = $this->Form->hour('Model.field', true);
+ $result = $this->Form->hour('Model.field', ['format' => 24]);
$expected = array(
- array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
+ array('select' => array('name' => 'Model[field][hour]')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '00')),

0 comments on commit 47a8ac4

Please sign in to comment.