multi-array optgroup and default_value

mixel - May 29, 2007 - 10:54
Project:Drupal
Version:5.x-dev
Component:forms system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

I’ve been experiencing a problem with creating a select with multi-array. It creates nicely “optgroup” but the ‘default_value’ seams to act not like I’ve hoped.
The question I have is: should I add this as a patch or do I violate another multi-array issue?

Here is a detailed explanation of the problem. If I give an array like:

array(peronal => array(first, test, test2), default => array(private, public, current));

And would like to set a default value as:

array(default => 1)

I would expect:

<optgroup label="personal">
 <option value="0">first</option>
 <option value="1">test</option>
 <option value="2">test2</option>
</optgroup>
<optgroup label="default">
 <option value="0">private</option>
 <option value="1" selected="selected">public</option>
 <option value="2">current</option>
</optgroup>

Bot get:

<optgroup label="personal">
 <option value="0">first</option>
 <option value="1" selected="selected">test</option>
</optgroup>
<optgroup label="default">
 <option value="0">private</option>
 <option value="1" selected="selected">public</option>
 <option value="2">current</option>
</optgroup>

Notice both ‘test” as ‘public’ is selected.

When you go to the “form_select_options” function in “from.inc”, you’ll see how the recursion is handled, I’ve played around to make the recursion work like I wanted, only needed to change:
$options .= form_select_options($element, $choice);
to:
if($value_is_array) {
 $rec_el = $element;
 $rec_el['#value'] = $element['#value'][$key];
 $options .= form_select_options($rec_el, $choice);
} else {
 $options .= form_select_options($element, $choice);
}

#1

chOP - May 13, 2008 - 23:46

I too have experienced problems using nested arrays of options in order to create OPTGROUPings, in that you cannot properly set a '#default_value' and the formAPI doesn't seem able to save state after submission. Fixing this problem would be great. I've abandoned the use of OPTGROUPs alltogether because it doesn't work right now in Drupal 5.x.

This is an important accessibility and usability issue. Has it been addressed in Drupal 6.x or 7.x yet?

Christopher
www.ourbrisbane.com

#2

queenielow - July 29, 2008 - 11:06

I think this may help.

I found this solution which i was looking for a long time.

$form['filter']['category']=array(
'#type' => 'select',
'#options'=>array('%'=>'Any','Categories'=>array('1'=>List1)),
'#title'=>t('Category Name'),
);

I works fine for me.. the 0 will be replaced with Categories.

 
 

Drupal is a registered trademark of Dries Buytaert.