title: Drupal 7 select optgroup and allow multiple selections
body: <p>I am using drupal 7 and need a form that provides a select box with optgroups and allows the user to select multiple items.</p>
  
  <p>I can create the #options array along with optgroup by using </p>
  
  <pre><code>array(
     ['Option Group 1']=&gt;array(
        [1]=&gt;'First Item',
        [2]=&gt;'Second Item',
     ),
     ['Option Group 2']=&gt;array(
        [3]=&gt;'Third Item',
     ),
  );
  </code></pre>
  
  <p>but if I then set</p>
  
  <pre><code>'#multiple'=&gt;TRUE
  </code></pre>
  
  <p>I just get a select full of checkboxes with array() as the list item title</p>
  
  <p>If I set</p>
  
  <pre><code>  '#attributes'=&gt;array('multiple'=&gt;'multiple')
  </code></pre>
  
  <p>The form looks correct but drupal does not recognise any of my selections and sends me back to the form saying I need to select an option from the list.</p>