7

Is there a way with the Form API to specify that one or more #options in a 'select' element should be disabled? I would like to insert a separator in my dropdown options that is not a selectable value.

1 Answer 1

8

If it's really a separator you're looking to add then it might make more semantic sense to use <optgroup>s to group your select options.

You can do this with the form API simply by wrapping each group you want in the <select> in an array, keyed by the title you want for the label:

$form['test'] = array(
  '#type' => 'select',
  '#title' => 'test',
  '#weight' => -10000,
  '#options' => array(
    'Group 1' => array(
      1 => 'Group 1 Item 1'
    ),
    'Group 2' => array(
      2 => 'Group 2 Item 1',
      3 => 'Group 2 Item 2'
    )
  )
);

That will produce a select list like this:

Select List

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.