Permalink
Browse files

Allowing FormHelper::month() to use a $monthNames array as values.

  • Loading branch information...
1 parent 988ab2d commit 034b5c435c9e3f2ab1006d1938a3ec3a6ff0df63 @renan renan committed Nov 2, 2009
Showing with 39 additions and 3 deletions.
  1. +6 −3 cake/libs/view/helpers/form.php
  2. +33 −0 cake/tests/cases/libs/view/helpers/form.test.php
@@ -653,7 +653,7 @@ function input($fieldName, $options = array()) {
$this->_inputDefaults,
$options
);
-
+
$modelKey = $this->model();
$fieldKey = $this->field();
if (!isset($this->fieldset[$modelKey])) {
@@ -1422,7 +1422,8 @@ function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $a
*
* Attributes:
*
- * - `monthNames` is set and 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.
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
*
@@ -1916,7 +1917,7 @@ function __generateOptions($name, $options = array()) {
}
break;
case 'month':
- if ($options['monthNames']) {
+ if ($options['monthNames'] === true) {
$data['01'] = __('January', true);
$data['02'] = __('February', true);
$data['03'] = __('March', true);
@@ -1929,6 +1930,8 @@ function __generateOptions($name, $options = array()) {
$data['10'] = __('October', true);
$data['11'] = __('November', true);
$data['12'] = __('December', true);
+ } else if (is_array($options['monthNames'])) {
+ $data = $options['monthNames'];
} else {
for ($m = 1; $m <= 12; $m++) {
$data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
@@ -4023,6 +4023,39 @@ function testMonth() {
'*/select',
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->month('Model.field', null, array('monthNames' => false));
+ $expected = array(
+ array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
+ array('option' => array('value' => '')),
+ '/option',
+ array('option' => array('value' => '01')),
+ '01',
+ '/option',
+ array('option' => array('value' => '02')),
+ '02',
+ '/option',
+ '*/select',
+ );
+ $this->assertTags($result, $expected);
+
+ $monthNames = array(
+ '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', null, array('monthNames' => $monthNames));
+ $expected = array(
+ array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
+ array('option' => array('value' => '')),
+ '/option',
+ array('option' => array('value' => '01')),
+ 'Jan',
+ '/option',
+ array('option' => array('value' => '02')),
+ 'Feb',
+ '/option',
+ '*/select',
+ );
+ $this->assertTags($result, $expected);
}
/**

0 comments on commit 034b5c4

Please sign in to comment.