Skip to content

Commit

Permalink
Get month() working with the new widget class.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 25, 2014
1 parent 6987857 commit 96560ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
51 changes: 26 additions & 25 deletions src/View/Helper/FormHelper.php
Expand Up @@ -1865,12 +1865,16 @@ public function day($fieldName = null, $options = []) {
); );
$options = $off + $options; $options = $off + $options;


if (isset($options['value'])) {
$options['val'] = $options['value'];
}

// If value is an integer reformat it. // If value is an integer reformat it.
if (isset($options['value']) && $options['value'] > 0 && $options['value'] < 31) { if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 31) {
$options['value'] = [ $options['val'] = [
'year' => date('Y'), 'year' => date('Y'),
'month' => date('m'), 'month' => date('m'),
'day' => (int)$options['value'] 'day' => (int)$options['val']
]; ];
} }
return $this->datetime($fieldName, $options); return $this->datetime($fieldName, $options);
Expand Down Expand Up @@ -1938,7 +1942,7 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
/** /**
* Returns a SELECT element for months. * Returns a SELECT element for months.
* *
* ### Attributes: * ### Options:
* *
* - `monthNames` - If false, 2 digit numbers will be used instead of text. * - `monthNames` - If false, 2 digit numbers will be used instead of text.
* If a array, the given array will be used. * If a array, the given array will be used.
Expand All @@ -1947,33 +1951,30 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
* - `value` The selected value of the input. * - `value` The selected value of the input.
* *
* @param string $fieldName Prefix name for the SELECT element * @param string $fieldName Prefix name for the SELECT element
* @param array $attributes Attributes for the select element * @param array $options Attributes for the select element
* @return string A generated month select dropdown. * @return string A generated month select dropdown.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
*/ */
public function month($fieldName, $attributes = array()) { public function month($fieldName, $options = array()) {
$attributes += array('empty' => true, 'value' => null); $off = array_diff($this->_datetimeParts, ['month']);
$attributes = $this->_dateTimeSelected('month', $fieldName, $attributes); $off = array_combine(
$off,
array_fill(0, count($off), false)
);
$options = $off + $options;


if (strlen($attributes['value']) > 2) { if (isset($options['value'])) {
$date = date_create($attributes['value']); $options['val'] = $options['value'];
$attributes['value'] = null;
if ($date) {
$attributes['value'] = $date->format('m');
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
} }
$defaults = array('monthNames' => true);
$attributes = array_merge($defaults, (array)$attributes);
$monthNames = $attributes['monthNames'];
unset($attributes['monthNames']);


return $this->select( if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 12) {
$fieldName . ".month", $options['val'] = [
$this->_generateOptions('month', array('monthNames' => $monthNames)), 'year' => date('Y'),
$attributes 'month' => (int)$options['val'],
); 'day' => date('d')
];
}
return $this->datetime($fieldName, $options);
} }


/** /**
Expand Down
25 changes: 13 additions & 12 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -5423,10 +5423,10 @@ public function testDateTimeLabelIdMatchesFirstInput() {
* @return void * @return void
*/ */
public function testMonth() { public function testMonth() {
$result = $this->Form->month('Model.field'); $result = $this->Form->month('Model.field', ['value' => '']);
$expected = array( $expected = array(
array('select' => array('name' => 'Model[field][month]')), array('select' => array('name' => 'Model[field][month]')),
array('option' => array('value' => '')), array('option' => array('value' => '', 'selected' => 'selected')),
'/option', '/option',
array('option' => array('value' => '01')), array('option' => array('value' => '01')),
date('F', strtotime('2008-01-01 00:00:00')), date('F', strtotime('2008-01-01 00:00:00')),
Expand All @@ -5438,10 +5438,10 @@ public function testMonth() {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$result = $this->Form->month('Model.field', array('empty' => true)); $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
$expected = array( $expected = array(
array('select' => array('name' => 'Model[field][month]')), array('select' => array('name' => 'Model[field][month]')),
array('option' => array('value' => '')), array('option' => array('selected' => 'selected', 'value' => '')),
'/option', '/option',
array('option' => array('value' => '01')), array('option' => array('value' => '01')),
date('F', strtotime('2008-01-01 00:00:00')), date('F', strtotime('2008-01-01 00:00:00')),
Expand All @@ -5453,30 +5453,31 @@ public function testMonth() {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$result = $this->Form->month('Model.field', array('monthNames' => false)); $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
$expected = array( $expected = array(
array('select' => array('name' => 'Model[field][month]')), array('select' => array('name' => 'Model[field][month]')),
array('option' => array('value' => '')), array('option' => array('selected' => 'selected', 'value' => '')),
'/option', '/option',
array('option' => array('value' => '01')), array('option' => array('value' => '01')),
'01', '1',
'/option', '/option',
array('option' => array('value' => '02')), array('option' => array('value' => '02')),
'02', '2',
'/option', '/option',
'*/select', '*/select',
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);


$monthNames = array( $monthNames = [
'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun', '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
'07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'); '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
$result = $this->Form->month('Model.field', array('monthNames' => $monthNames)); ];
$result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
$expected = array( $expected = array(
array('select' => array('name' => 'Model[field][month]')), array('select' => array('name' => 'Model[field][month]')),
array('option' => array('value' => '')), array('option' => array('value' => '')),
'/option', '/option',
array('option' => array('value' => '01')), array('option' => array('value' => '01', 'selected' => 'selected')),
'Jan', 'Jan',
'/option', '/option',
array('option' => array('value' => '02')), array('option' => array('value' => '02')),
Expand Down

0 comments on commit 96560ea

Please sign in to comment.