Skip to content

Commit

Permalink
Adding 'orderYear' option for FormHelper::year to allow control over …
Browse files Browse the repository at this point in the history
…ordering of select options
  • Loading branch information
ADmad committed Jan 27, 2010
1 parent 57997e7 commit c9e047c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
22 changes: 15 additions & 7 deletions cake/libs/view/helpers/form.php
Expand Up @@ -306,7 +306,7 @@ function create($model = null, $options = array()) {
*
* {{{
* array usage:
*
*
* array('label' => 'save'); value="save"
* array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
* array('name' => 'Whatever'); value="Submit" name="Whatever"
Expand Down Expand Up @@ -389,7 +389,7 @@ function secure($fields = array()) {
}

/**
* Determine which fields of a form should be used for hash.
* Determine which fields of a form should be used for hash.
* Populates $this->fields
*
* @param mixed $field Reference to field to be secured
Expand Down Expand Up @@ -644,7 +644,7 @@ function inputs($fields = null, $blacklist = null) {
*
* - `type` - Force the type of widget you want. e.g. `type => 'select'`
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label()
* - `div` - Either `false` to disable the div, or an array of options for the div.
* - `div` - Either `false` to disable the div, or an array of options for the div.
* See HtmlHelper::div() for more options.
* - `options` - for widgets that take options e.g. radio, select
* - `error` - control the error message that is produced
Expand Down Expand Up @@ -1412,6 +1412,8 @@ function day($fieldName, $selected = null, $attributes = array()) {
*
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
* - `orderYear` - Ordering of year values in select options.
* Possible values 'asc', 'desc'. Default 'desc'
*
* @param string $fieldName Prefix name for the SELECT element
* @param integer $minYear First year in sequence
Expand Down Expand Up @@ -1446,9 +1448,13 @@ function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $a
} elseif ($selected === false) {
$selected = null;
}
$yearOptions = array('min' => $minYear, 'max' => $maxYear);
$yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
if (isset($attributes['orderYear'])) {
$yearOptions['order'] = $attributes['orderYear'];
unset($attributes['orderYear']);
}
return $this->select(
$fieldName . ".year", $this->__generateOptions('year', $yearOptions),
$fieldName . '.year', $this->__generateOptions('year', $yearOptions),
$selected, $attributes
);
}
Expand Down Expand Up @@ -1862,7 +1868,7 @@ function __selectOptions($elements = array(), $selected = null, $parents = array

if ($name !== null) {
if (
(!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) ||
(!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) ||
($selectedIsArray && in_array($name, $selected))
) {
if ($attributes['style'] === 'checkbox') {
Expand Down Expand Up @@ -2005,7 +2011,9 @@ function __generateOptions($name, $options = array()) {
for ($i = $min; $i <= $max; $i++) {
$data[$i] = $i;
}
$data = array_reverse($data, true);
if ($options['order'] != 'asc') {
$data = array_reverse($data, true);
}
break;
}
$this->__options[$name] = $data;
Expand Down
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -4465,6 +4465,21 @@ function testYear() {
);
$this->assertTags($result, $expected);

$result = $this->Form->year('Model.field', 2006, 2007, null, array('orderYear' => 'asc'));
$expected = array(
array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '2006')),
'2006',
'/option',
array('option' => array('value' => '2007')),
'2007',
'/option',
'/select',
);
$this->assertTags($result, $expected);

$this->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year'));
$expected = array(
Expand Down

0 comments on commit c9e047c

Please sign in to comment.