Skip to content
Permalink
Browse files

Add start/end options for hours option.

This allows developers to limit the hour options that are displayed much
like you can limit the number of years displayed.

Refs #2701
  • Loading branch information...
markstory committed Jan 26, 2014
1 parent 3176422 commit 69fc3b223ec720b20af005e75d2f3bbeee32209f
Showing with 112 additions and 3 deletions.
  1. +21 −3 src/View/Input/DateTime.php
  2. +91 −0 tests/TestCase/View/Input/DateTimeTest.php
@@ -320,21 +320,39 @@ protected function _hourSelect($options = []) {
'name' => '',
'val' => null,
'format' => 24,
'start' => null,
'end' => null,
'leadingZeroKey' => true,
'leadingZeroValue' => false,
];
$is24 = $options['format'] == 24;
$defaultEnd = $is24 ? 24 : 12;
$options['start'] = max(1, $options['start']);
$options['end'] = min($defaultEnd, $options['end']);
if ($options['end'] === null) {
$options['end'] = $defaultEnd;
}
if (!$is24 && $options['val'] > 12) {
$options['val'] = sprintf('%02d', $options['val'] - 12);
}
if (empty($options['options'])) {
$end = $is24 ? 24 : 12;
$options['options'] = $this->_generateNumbers(1, $end, $options);
$options['options'] = $this->_generateNumbers(
$options['start'],
$options['end'],
$options
);
}
unset($options['format'], $options['leadingZeroKey'], $options['leadingZeroValue']);
unset(
$options['end'], $options['start'],
$options['format'], $options['leadingZeroKey'],
$options['leadingZeroValue']
);
return $this->_select->render($options);
}
@@ -374,6 +374,49 @@ public function testRenderDayWidget() {
$this->assertTags($result, $expected);
}
/**
* Test rendering the hour picker in 24 hour mode.
*
* @return void
*/
public function testRenderHourWidget24StartAndEnd() {
$now = new \DateTime('2010-09-09 13:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'month' => false,
'day' => false,
'hour' => [
'start' => 8,
'end' => 16
],
'minute' => false,
'second' => false,
'val' => $now,
]);
$this->assertContains('<select name="date[hour]">', $result);
$this->assertNotContains(
'<option value="01">1</option>',
$result,
'no 1 am'
);
$this->assertNotContains(
'<option value="07">7</option>',
$result,
'contain 7'
);
$this->assertContains(
'<option value="13" selected="selected">13</option>',
$result,
'selected value present'
);
$this->assertNotContains(
'<option value="17">17</option>',
$result,
'contains 17 hours'
);
}
/**
* Test rendering the hour picker in 24 hour mode.
*
@@ -469,6 +512,54 @@ public function testRenderHourWidget12() {
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
}
/**
* Test rendering the hour picker in 12 hour mode.
*
* @return void
*/
public function testRenderHourWidget12StartAndEnd() {
$now = new \DateTime('2010-09-09 13:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'month' => false,
'day' => false,
'hour' => [
'start' => 8,
'end' => 12
],
'minute' => false,
'second' => false,
'val' => $now,
]);
$this->assertContains('<select name="date[hour]">', $result);
$this->assertContains(
'<option value="08">8</option>',
$result,
'contains 8am'
);
$this->assertContains(
'<option value="12">12</option>',
$result,
'contains 8am'
);
$this->assertNotContains(
'<option value="01">1</option>',
$result,
'no 1 am'
);
$this->assertNotContains(
'<option value="07">7</option>',
$result,
'contain 7'
);
$this->assertNotContains(
'<option value="13" selected="selected">13</option>',
$result,
'selected value present'
);
}
/**
* Test rendering the minute widget with no options.
*

0 comments on commit 69fc3b2

Please sign in to comment.
You can’t perform that action at this time.