<p>Few things - you need to add the options to the render function so they are passed into the template. Also, check what I did below for the <code>$widthsOptions</code>. Each option needs a label and value key/value pair.</p>

<pre><code>public function getInputHtml($name, $value)
{
    // call our service layer to get a current list of widths
    $widths = craft()-&gt;columnWidth_widths-&gt;getAllWidths();

    // create an array for the widths 
    $widthsOptions = array();

    // populate the $widthsOptions array with $widths
    foreach ($widths as $width )
    {
        $widthsOptions[] = array(
            'label' =&gt; $width-&gt;name, //this should be whatever you want to display in dropdown
            'value' =&gt; $width-&gt;name //this should be the value you want passed to the database
        );
    }

    return craft()-&gt;templates-&gt;render('columnwidth/_fieldtypes/widths', array(
        'name'      =&gt; $name,
        'values'    =&gt; $value,
        'options'   =&gt; $widthsOptions
    ));
}
</code></pre>

<p>Your field type template should be something like this:</p>

<pre><code>{% import "_includes/forms/" as forms %}
​
{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: options,
    value: values
}) }}
</code></pre>

CC BY-SA 3.0
2015-10-08T13:59:23.967Z
what would cause the selected value to not save? on save the value keeps reverting back to the first value.
Mark Busnelli Jr
CC BY-SA 3.0
2015-10-08T14:02:20.210Z
Is the value saving correctly in the database and just not showing up as the selected item, or is it not saving at all?
Aaron Berkowitz
CC BY-SA 3.0
2015-10-08T14:06:45.327Z
It's saving, so at least there's that :)
Mark Busnelli Jr
CC BY-SA 3.0
2015-10-08T14:09:22.890Z
Just updated the answer - you need to have `value: values` in the selectField macro options so it knows which value is currently saved in the database.
Aaron Berkowitz
CC BY-SA 3.0
2015-10-08T14:10:50.117Z
Nice, really wish there was some documentation on this besides rummaging around in the `forms` :)
Mark Busnelli Jr
CC BY-SA 3.0
2015-10-08T14:11:41.623Z
any ideas on getting it to display in my templates now? :) that's my final piece! new question perhaps?
Mark Busnelli Jr
CC BY-SA 3.0
2015-10-08T14:13:15.483Z
What are you trying to display? Since this is a fieldtype, all you need to is add it to a section/entry type just as you would a normal field. You can then call it in your templates: `{{entry.yourFieldHandle}}`
Aaron Berkowitz
CC BY-SA 3.0
2015-10-07T21:48:24.980Z
2015-10-08T14:08:54.840Z
2015-10-08T14:08:54.840Z
Aaron Berkowitz
<p>I'm attempting to finish my first <code>fieldtype</code> plugin (how exciting!) but having an issue with displaying options in a <code>select</code> dropdown in an <code>entry</code>.</p>

<p>My <code>ColumnWidth_WidthsFieldType</code> has this:</p>

<pre><code>&lt;?php

namespace Craft;

/**
 * Widths Fieldtype
 *
 * Allows entries to select associated widths
 */
class ColumnWidth_WidthsFieldType extends BaseFieldType
{
    /**
     * Get the name of this fieldtype
     */
    public function getName()
    {
        return Craft::t('Column Widths');
    }

    /**
     * Get this fieldtype's column type.
     *
     * @return mixed
     */
    public function defineContentAttribute()
    {
        // "Mixed" represents a "text" column type, which can be used to store arrays etc.
        return AttributeType::Mixed;
    }

    /**
     * Get this fieldtype's form HTML
     *
     * @param  string $name
     * @param  mixed  $value
     * @return string
     */
    public function getInputHtml($name, $value)
    {
        // call our service layer to get a current list of widths
        $widths = craft()-&gt;columnWidth_widths-&gt;getAllWidths();

        // create an array for the widths 
        $widthsOptions = array();

        // populate the $widthsOptions array with $widths
        foreach ($widthsOptions as $widths )
        {
            $widthsOptions[$widths-&gt;id] =  $widths-&gt;name;
        }

        return craft()-&gt;templates-&gt;render('columnwidth/_fieldtypes/widths', array(
            'name'      =&gt; $name,
            'values'    =&gt; $value,
        ));
    }
}
</code></pre>

<p>My <code>columnwidth/_fieldtypes/widths</code> has this:</p>

<pre><code>{% import "_includes/forms/" as forms %}
​
{% set widths = craft.columnwidth.getAllWidths %}

{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: widths
}) }}
</code></pre>

<p>But I'm getting this error:</p>

<p><code>Craft\ColumnWidth_WidthsModel and its behaviors do not have a method or closure named "optgroup".</code></p>

<p>The only post I found was here:</p>

<p><a href="https://craftcms.stackexchange.com/questions/4491/displaying-a-list-of-fields-in-a-form-select">Displaying a list of fields in a form select</a></p>

<p>I'm sure I'm missing something and any help is appreciated!</p>

<plugin-development><plugins><fieldtypes><custom-fieldtype>
Model and its behaviors do not have a method or closure named "optgroup"
# Model and its behaviors do not have a method or closure named "optgroup"

(Asked by: username_0 on 2015-10-07)

I'm attempting to finish my first fieldtype plugin (how exciting!) but having an issue with displaying options in a select dropdown in an entry.
My ColumnWidth_WidthsFieldType has this:
columnWidth_widths->getAllWidths();

        // create an array for the widths 
        $widthsOptions = array();

        // populate the $widthsOptions array with $widths
        foreach ($widthsOptions as $widths )
        {
            $widthsOptions[$widths->id] =  $widths->name;
        }

        return craft()->templates->render('columnwidth/_fieldtypes/widths', array(
            'name'      => $name,
            'values'    => $value,
        ));
    }
}

My columnwidth/_fieldtypes/widths has this:
{% import "_includes/forms/" as forms %}
​
{% set widths = craft.columnwidth.getAllWidths %}

{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: widths
}) }}

But I'm getting this error:
Craft\ColumnWidth_WidthsModel and its behaviors do not have a method or closure named "optgroup".
The only post I found was here:
Displaying a list of fields in a form select
I'm sure I'm missing something and any help is appreciated!

**Tags:** plugin-development, plugins, fieldtypes, custom-fieldtype

---

## 1 answers

### Answer by username_1 on 2015-10-07. Score: 2 | Accepted answer

Few things - you need to add the options to the render function so they are passed into the template. Also, check what I did below for the $widthsOptions. Each option needs a label and value key/value pair.
public function getInputHtml($name, $value)
{
    // call our service layer to get a current list of widths
    $widths = craft()->columnWidth_widths->getAllWidths();

    // create an array for the widths 
    $widthsOptions = array();

    // populate the $widthsOptions array with $widths
    foreach ($widths as $width )
    {
        $widthsOptions[] = array(
            'label' => $width->name, //this should be whatever you want to display in dropdown
            'value' => $width->name //this should be the value you want passed to the database
        );
    }

    return craft()->templates->render('columnwidth/_fieldtypes/widths', array(
        'name'      => $name,
        'values'    => $value,
        'options'   => $widthsOptions
    ));
}

Your field type template should be something like this:
{% import "_includes/forms/" as forms %}
​
{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: options,
    value: values
}) }}

> **Comments:**
> **username_0**: what would cause the selected value to not save? on save the value keeps reverting back to the first value.  
> **username_1**: Is the value saving correctly in the database and just not showing up as the selected item, or is it not saving at all?  
> **username_0**: It's saving, so at least there's that :)  
> **username_1**: Just updated the answer - you need to have `value: values` in the selectField macro options so it knows which value is currently saved in the database.  
> **username_0**: Nice, really wish there was some documentation on this besides rummaging around in the `forms` :)  
> **username_0**: any ideas on getting it to display in my templates now? :) that's my final piece! new question perhaps?  
> **username_1**: What are you trying to display? Since this is a fieldtype, all you need to is add it to a section/entry type just as you would a normal field. You can then call it in your templates: `{{entry.yourFieldHandle}}`  

---

