Skip to content

Commit 2eb552f

Browse files
committed
Add time() to FormHelper.
Add concrete time input methods to FormHelper. This will let input() also generate time type inputs.
1 parent eee474b commit 2eb552f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/View/Helper/FormHelper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,45 @@ protected function _datetimeOptions($options) {
19721972
return $options;
19731973
}
19741974

1975+
/**
1976+
* Generate time inputs.
1977+
*
1978+
* ### Options:
1979+
*
1980+
* - `interval` The interval for the minutes select. Defaults to 1
1981+
* - `empty` - If true, the empty select option is shown. If a string,
1982+
* that string is displayed as the empty element.
1983+
* - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
1984+
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
1985+
* matching the field name will override this value. If no default is provided `time()` will be used.
1986+
* - `timeFormat` The time format to use, either 12 or 24.
1987+
* - `second` Set to true to enable seconds drop down.
1988+
*
1989+
* To control the order of inputs, and any elements/content between the inputs you
1990+
* can override the `dateWidget` template. By default the `dateWidget` template is:
1991+
*
1992+
* `{{month}}{{day}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}`
1993+
*
1994+
* @param string $fieldName Prefix name for the SELECT element
1995+
* @param array $options Array of Options
1996+
* @return string Generated set of select boxes for time formats chosen.
1997+
*/
1998+
public function time($fieldName, $options = []) {
1999+
$options += [
2000+
'empty' => true,
2001+
'value' => null,
2002+
'interval' => 1,
2003+
'round' => null,
2004+
'timeFormat' => 12,
2005+
'second' => false,
2006+
];
2007+
$options['year'] = $options['month'] = $options['day'] = false;
2008+
$options = $this->_initInputField($fieldName, $options);
2009+
$options = $this->_datetimeOptions($options);
2010+
2011+
return $this->widget('datetime', $options);
2012+
}
2013+
19752014
/**
19762015
* Sets field defaults and adds field to form security input hash.
19772016
* Will also add the error class if the field contains validation errors.

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,6 +3789,37 @@ public function testCheckboxHiddenField() {
37893789
$this->assertTags($result, $expected);
37903790
}
37913791

3792+
/**
3793+
* Test the time type.
3794+
*
3795+
* @return void
3796+
*/
3797+
public function testTime() {
3798+
$result = $this->Form->time('start_time', array(
3799+
'timeFormat' => 12,
3800+
'interval' => 5,
3801+
'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
3802+
));
3803+
$this->assertContains('<option value="04" selected="selected">4</option>', $result);
3804+
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
3805+
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
3806+
$this->assertNotContains('year', $result);
3807+
$this->assertNotContains('month', $result);
3808+
$this->assertNotContains('day', $result);
3809+
3810+
$result = $this->Form->time('start_time', array(
3811+
'timeFormat' => 12,
3812+
'interval' => 5,
3813+
'value' => '2014-03-08 16:30:00'
3814+
));
3815+
$this->assertContains('<option value="04" selected="selected">4</option>', $result);
3816+
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
3817+
$this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
3818+
$this->assertNotContains('year', $result);
3819+
$this->assertNotContains('month', $result);
3820+
$this->assertNotContains('day', $result);
3821+
}
3822+
37923823
/**
37933824
* testDateTime method
37943825
*

0 commit comments

Comments
 (0)