#default_value doesn't work with SELECT OPTGROUPs
| Project: | Drupal |
| Version: | 5.5 |
| Component: | forms system |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | by design |
Jump to:
G'day,
I have noticed that the '#default_value' property doesn't seem to work as expected with form elements of '#type'=>'select' when the '#options' property contains a nested array for the output of option groups - <OPTGROUP/>.
In the following code the '#default_value' does not work as expected:
<?php
$day_in_seconds = 60*60*24;
// Calculate dates for timeframes.
$option_today = date('d-m-Y');
$option_today1 = date('d-m-Y', strtotime('+1 day'));
$option_today2 = date('d-m-Y', strtotime('+2 day'));
$option_today6 = date('d-m-Y', strtotime('+6 day'));
$option_weekend_begins = date('d-m-Y', (strtotime('sunday') - (2 * $day_in_seconds) < strtotime('today') ? strtotime('today') : strtotime('sunday') - 2 * $day_in_seconds));
$option_weekend_ends = date('d-m-Y',strtotime('sunday'));
// Set option values corresponding to their labels.
$options_times = array(
'single' => array(
$option_today . ',' . $option_today => t('Today'),
$option_today1 . ',' . $option_today1 => t('Tomorrow'),
),
'multiple' => array(
$option_today . ',' => t('From today'),
$option_today . ',' . $option_today2 => t('Next three days'),
$option_today . ',' . $option_today6 => t('Next seven days'),
$option_weekend_begins . ',' . $option_weekend_ends => t('This weekend'),
),
t('My dates (from, to)'),
);
$form['daterange'] = array(
'#type' => 'select',
'#title' => t('Select date range'),
'#options' => $options_times,
'#default_value' => array_search(t('Next seven days'), $options_times),
'#prefix' => '<div id="edit-daterange-container">',
'#suffix' => '</div>',
'#required' => FALSE,
);
?>Whereas, if I remove the nesting from the '#options' property array, and thereby removing the option groupings - <OPTGROUP/>s - the '#default_value' property works as you might expect:
<?php
$day_in_seconds = 60*60*24;
// Calculate dates for timeframes.
$option_today = date('d-m-Y');
$option_today1 = date('d-m-Y', strtotime('+1 day'));
$option_today2 = date('d-m-Y', strtotime('+2 day'));
$option_today6 = date('d-m-Y', strtotime('+6 day'));
$option_weekend_begins = date('d-m-Y', (strtotime('sunday') - (2 * $day_in_seconds) < strtotime('today') ? strtotime('today') : strtotime('sunday') - 2 * $day_in_seconds));
$option_weekend_ends = date('d-m-Y',strtotime('sunday'));
// Set option values corresponding to their labels.
$options_times = array(
// 'single' => array(
$option_today . ',' . $option_today => t('Today'),
$option_today1 . ',' . $option_today1 => t('Tomorrow'),
// ),
// 'multiple' => array(
$option_today . ',' => t('From today'),
$option_today . ',' . $option_today2 => t('Next three days'),
$option_today . ',' . $option_today6 => t('Next seven days'),
$option_weekend_begins . ',' . $option_weekend_ends => t('This weekend'),
// ),
t('My dates (from, to)'),
);
$form['daterange'] = array(
'#type' => 'select',
'#title' => t('Select date range'),
'#options' => $options_times,
'#default_value' => array_search(t('Next seven days'), $options_times),
'#prefix' => '<div id="edit-daterange-container">',
'#suffix' => '</div>',
'#required' => FALSE,
);
?>If I'm mistaken and this is not a bug, but a problem with my usage of '#default_value' for form elements of '#type'=>'select', please let me know where I'm going wrong.
Thanks for your consideration and/or help,
Christopher
(ourbrisbane.com)

#1
The problem is not with Drupal, it is with your code. Please read the documents on array_search()— it is not a recursive function.