Skip to content

Commit c9e047c

Browse files
committed
Adding 'orderYear' option for FormHelper::year to allow control over ordering of select options
1 parent 57997e7 commit c9e047c

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

cake/libs/view/helpers/form.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ function create($model = null, $options = array()) {
306306
*
307307
* {{{
308308
* array usage:
309-
*
309+
*
310310
* array('label' => 'save'); value="save"
311311
* array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
312312
* array('name' => 'Whatever'); value="Submit" name="Whatever"
@@ -389,7 +389,7 @@ function secure($fields = array()) {
389389
}
390390

391391
/**
392-
* Determine which fields of a form should be used for hash.
392+
* Determine which fields of a form should be used for hash.
393393
* Populates $this->fields
394394
*
395395
* @param mixed $field Reference to field to be secured
@@ -644,7 +644,7 @@ function inputs($fields = null, $blacklist = null) {
644644
*
645645
* - `type` - Force the type of widget you want. e.g. `type => 'select'`
646646
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label()
647-
* - `div` - Either `false` to disable the div, or an array of options for the div.
647+
* - `div` - Either `false` to disable the div, or an array of options for the div.
648648
* See HtmlHelper::div() for more options.
649649
* - `options` - for widgets that take options e.g. radio, select
650650
* - `error` - control the error message that is produced
@@ -1412,6 +1412,8 @@ function day($fieldName, $selected = null, $attributes = array()) {
14121412
*
14131413
* - `empty` - If true, the empty select option is shown. If a string,
14141414
* that string is displayed as the empty element.
1415+
* - `orderYear` - Ordering of year values in select options.
1416+
* Possible values 'asc', 'desc'. Default 'desc'
14151417
*
14161418
* @param string $fieldName Prefix name for the SELECT element
14171419
* @param integer $minYear First year in sequence
@@ -1446,9 +1448,13 @@ function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $a
14461448
} elseif ($selected === false) {
14471449
$selected = null;
14481450
}
1449-
$yearOptions = array('min' => $minYear, 'max' => $maxYear);
1451+
$yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
1452+
if (isset($attributes['orderYear'])) {
1453+
$yearOptions['order'] = $attributes['orderYear'];
1454+
unset($attributes['orderYear']);
1455+
}
14501456
return $this->select(
1451-
$fieldName . ".year", $this->__generateOptions('year', $yearOptions),
1457+
$fieldName . '.year', $this->__generateOptions('year', $yearOptions),
14521458
$selected, $attributes
14531459
);
14541460
}
@@ -1862,7 +1868,7 @@ function __selectOptions($elements = array(), $selected = null, $parents = array
18621868

18631869
if ($name !== null) {
18641870
if (
1865-
(!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) ||
1871+
(!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) ||
18661872
($selectedIsArray && in_array($name, $selected))
18671873
) {
18681874
if ($attributes['style'] === 'checkbox') {
@@ -2005,7 +2011,9 @@ function __generateOptions($name, $options = array()) {
20052011
for ($i = $min; $i <= $max; $i++) {
20062012
$data[$i] = $i;
20072013
}
2008-
$data = array_reverse($data, true);
2014+
if ($options['order'] != 'asc') {
2015+
$data = array_reverse($data, true);
2016+
}
20092017
break;
20102018
}
20112019
$this->__options[$name] = $data;

cake/tests/cases/libs/view/helpers/form.test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4465,6 +4465,21 @@ function testYear() {
44654465
);
44664466
$this->assertTags($result, $expected);
44674467

4468+
$result = $this->Form->year('Model.field', 2006, 2007, null, array('orderYear' => 'asc'));
4469+
$expected = array(
4470+
array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
4471+
array('option' => array('value' => '')),
4472+
'/option',
4473+
array('option' => array('value' => '2006')),
4474+
'2006',
4475+
'/option',
4476+
array('option' => array('value' => '2007')),
4477+
'2007',
4478+
'/option',
4479+
'/select',
4480+
);
4481+
$this->assertTags($result, $expected);
4482+
44684483
$this->data['Contact']['published'] = '';
44694484
$result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year'));
44704485
$expected = array(

0 commit comments

Comments
 (0)