Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T13:59:23.967Z
Answers.Text: what would cause the selected value to not save? on save the value keeps reverting back to the first value.
Answers.UserDisplayName: Mark Busnelli Jr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:02:20.210Z
Answers.Text: Is the value saving correctly in the database and just not showing up as the selected item, or is it not saving at all?
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:06:45.327Z
Answers.Text: It's saving, so at least there's that :)
Answers.UserDisplayName: Mark Busnelli Jr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:09:22.890Z
Answers.Text: 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.
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:10:50.117Z
Answers.Text: Nice, really wish there was some documentation on this besides rummaging around in the `forms` :)
Answers.UserDisplayName: Mark Busnelli Jr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:11:41.623Z
Answers.Text: any ideas on getting it to display in my templates now? :) that's my final piece! new question perhaps?
Answers.UserDisplayName: Mark Busnelli Jr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:13:15.483Z
Answers.Text: 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}}`
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-07T21:48:24.980Z
Answers.LastActivityDate: 2015-10-08T14:08:54.840Z
Answers.LastEditDate: 2015-10-08T14:08:54.840Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <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>
  
Tags: <plugin-development><plugins><fieldtypes><custom-fieldtype>
Title: Model and its behaviors do not have a method or closure named "optgroup"
ThreadText: # 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:
  
  ```
  <?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()->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](https://craftcms.stackexchange.com/questions/4491/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}}`  
  
  ---
  
  