Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Get month() working with the new widget class.

  • Loading branch information...
commit 96560ea44384f6b0c81394d2b11d864b8dd3738d 1 parent 6987857
Mark Story markstory authored
51 src/View/Helper/FormHelper.php
View
@@ -1865,12 +1865,16 @@ public function day($fieldName = null, $options = []) {
);
$options = $off + $options;
+ if (isset($options['value'])) {
+ $options['val'] = $options['value'];
+ }
+
// If value is an integer reformat it.
- if (isset($options['value']) && $options['value'] > 0 && $options['value'] < 31) {
- $options['value'] = [
+ if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 31) {
+ $options['val'] = [
'year' => date('Y'),
'month' => date('m'),
- 'day' => (int)$options['value']
+ 'day' => (int)$options['val']
];
}
return $this->datetime($fieldName, $options);
@@ -1938,7 +1942,7 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
/**
* Returns a SELECT element for months.
*
- * ### Attributes:
+ * ### Options:
*
* - `monthNames` - If false, 2 digit numbers will be used instead of text.
* If a array, the given array will be used.
@@ -1947,33 +1951,30 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
* - `value` The selected value of the input.
*
* @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.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
*/
- public function month($fieldName, $attributes = array()) {
- $attributes += array('empty' => true, 'value' => null);
- $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
+ public function month($fieldName, $options = array()) {
+ $off = array_diff($this->_datetimeParts, ['month']);
+ $off = array_combine(
+ $off,
+ array_fill(0, count($off), false)
+ );
+ $options = $off + $options;
- if (strlen($attributes['value']) > 2) {
- $date = date_create($attributes['value']);
- $attributes['value'] = null;
- if ($date) {
- $attributes['value'] = $date->format('m');
- }
- } elseif ($attributes['value'] === false) {
- $attributes['value'] = null;
+ if (isset($options['value'])) {
+ $options['val'] = $options['value'];
}
- $defaults = array('monthNames' => true);
- $attributes = array_merge($defaults, (array)$attributes);
- $monthNames = $attributes['monthNames'];
- unset($attributes['monthNames']);
- return $this->select(
- $fieldName . ".month",
- $this->_generateOptions('month', array('monthNames' => $monthNames)),
- $attributes
- );
+ if (isset($options['val']) && $options['val'] > 0 && $options['val'] <= 12) {
+ $options['val'] = [
+ 'year' => date('Y'),
+ 'month' => (int)$options['val'],
+ 'day' => date('d')
+ ];
+ }
+ return $this->datetime($fieldName, $options);
}
/**
25 tests/TestCase/View/Helper/FormHelperTest.php
View
@@ -5423,10 +5423,10 @@ public function testDateTimeLabelIdMatchesFirstInput() {
* @return void
*/
public function testMonth() {
- $result = $this->Form->month('Model.field');
+ $result = $this->Form->month('Model.field', ['value' => '']);
$expected = array(
array('select' => array('name' => 'Model[field][month]')),
- array('option' => array('value' => '')),
+ array('option' => array('value' => '', 'selected' => 'selected')),
'/option',
array('option' => array('value' => '01')),
date('F', strtotime('2008-01-01 00:00:00')),
@@ -5438,10 +5438,10 @@ public function testMonth() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->month('Model.field', array('empty' => true));
+ $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
$expected = array(
array('select' => array('name' => 'Model[field][month]')),
- array('option' => array('value' => '')),
+ array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '01')),
date('F', strtotime('2008-01-01 00:00:00')),
@@ -5453,30 +5453,31 @@ public function testMonth() {
);
$this->assertTags($result, $expected);
- $result = $this->Form->month('Model.field', array('monthNames' => false));
+ $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
$expected = array(
array('select' => array('name' => 'Model[field][month]')),
- array('option' => array('value' => '')),
+ array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '01')),
- '01',
+ '1',
'/option',
array('option' => array('value' => '02')),
- '02',
+ '2',
'/option',
'*/select',
);
$this->assertTags($result, $expected);
- $monthNames = array(
+ $monthNames = [
'01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
- '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
- $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
+ '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
+ ];
+ $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
$expected = array(
array('select' => array('name' => 'Model[field][month]')),
array('option' => array('value' => '')),
'/option',
- array('option' => array('value' => '01')),
+ array('option' => array('value' => '01', 'selected' => 'selected')),
'Jan',
'/option',
array('option' => array('value' => '02')),
Please sign in to comment.
Something went wrong with that request. Please try again.