<p>I had the same issue and managed to get it working like so:</p>

<pre><code>{{ forms.selectField({
    options :[
     {optgroup:'North Island'},
     {label : 'Foo', value: 'foo'},
     {label : 'Bar', value: 'bar'},
     {optgroup:'South Island'},
     {label : 'Baz', value: 'baz'}
    ],
    name: name,
    value : value
})}}
</code></pre>

<p>So I guess in php (if working from a plugin) your array would need to look like:</p>

<pre><code>array(
 array('optgroup' =&gt; 'North Island'),
 array('label' =&gt; 'Foo', 'value' =&gt; 'foo'),
 array('label' =&gt; 'Bar', 'value' =&gt; 'bar')
 array('optgroup' =&gt; 'South Island'),
 array('label' =&gt; 'Baz', 'value' =&gt; 'baz')
);
</code></pre>

<p>This then would output it like this in the admin area</p>

<p><a href="https://i.sstatic.net/QM2aj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QM2aj.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-08-14T13:01:47.423Z
2015-08-14T13:01:47.423Z
Alec Ritson
<p>I'm trying to list out a select element with option groups and options using the awesome for macro that Pixel and Tonic created.</p>

<p>I have imported the macros and am calling the select one like so:</p>

<pre><code>{{ forms.select({
          id: 'ccs',
          label: "Collection store:",
          class: '',
          name: 'fulfilmentStore',
          hasOptgroups: true,
          options: [
            {'optgroup':'North Island', 'value': 'NI1',},
            {'optgroup':'South Island'}
          ],
        }) }}
</code></pre>

<p>I pretty sure I have to have another lot of options sitting under the optGroup but can't get the syntax right.</p>

<p>Thanks in advance.</p>

<macro>
How can I add an option group to the select Macro from CraftCMS forms macros
# How can I add an option group to the select Macro from CraftCMS forms macros

(Asked by: username_0 on 2015-06-24)

I'm trying to list out a select element with option groups and options using the awesome for macro that Pixel and Tonic created.
I have imported the macros and am calling the select one like so:
{{ forms.select({
          id: 'ccs',
          label: "Collection store:",
          class: '',
          name: 'fulfilmentStore',
          hasOptgroups: true,
          options: [
            {'optgroup':'North Island', 'value': 'NI1',},
            {'optgroup':'South Island'}
          ],
        }) }}

I pretty sure I have to have another lot of options sitting under the optGroup but can't get the syntax right.
Thanks in advance.

**Tags:** macro

---

## 1 answers

### Answer by username_1 on 2015-08-14. Score: 2

I had the same issue and managed to get it working like so:
{{ forms.selectField({
    options :[
     {optgroup:'North Island'},
     {label : 'Foo', value: 'foo'},
     {label : 'Bar', value: 'bar'},
     {optgroup:'South Island'},
     {label : 'Baz', value: 'baz'}
    ],
    name: name,
    value : value
})}}

So I guess in php (if working from a plugin) your array would need to look like:
array(
 array('optgroup' => 'North Island'),
 array('label' => 'Foo', 'value' => 'foo'),
 array('label' => 'Bar', 'value' => 'bar')
 array('optgroup' => 'South Island'),
 array('label' => 'Baz', 'value' => 'baz')
);

This then would output it like this in the admin area


---

