Select field in custom module

edited February 2011 in Modules
I have a simple custom module named "Supports" and it's working fine. I have a field called "Category" and in the admin, I want to be able to change it from an input text field to a select field so I can limit what the user enters when they create a new Support. Can anyone tell me how to implement this or point me in the right direction?

Comments

  • edited 5:34AM
    hi JavaJunky,

    I think you can call the form function in your "Support" module to overwrite it.

    function form_fields($values = array()) { $fields = parent::form_fields($values); $options = array('1'=>'Option1','2'=>'Option2'); //this should supply list of options $fields['Category'] = array('label' => 'Category', 'type' => 'select', 'options' => $options, 'value' => '', 'mode' => 'single'); return $fields; }

    you can check out the sample code on category module.
  • edited 5:34AM
    Thank you, knight! Worked perfectly. I figured it was something like that. I just didn't know the syntax.

    Where is the category module you refer to located, so I can see more sample code?
  • edited 5:34AM
    Thanks, again!
  • edited 5:34AM
    Does anyone know if it's possible to set one of the options to be selected? For example, I have Support A saved with Option2. When I go back to edit it, I want Option2 to be selected. Nothing I've tried worked.
  • edited 5:34AM
    The "value" property can be set in a field to select it's value. However, for modules, that will be overwritten by any saved value. From the sounds of it, Option2 should be selected if the value being saved to the database is 2. What value is getting saved into the database?
  • edited 5:34AM
    You're right. It was because one of my options was:
    'Profile & Settings'=>'Profile & Settings',

    When it outputted the html for the select, it converted ampersand in the value of that option to '&'. I changed the option to 'and' instead. That fixed it.
  • edited 5:34AM
    So the value in the database had the '&' but the option value got translated to '&'?
  • edited February 2011
    This is an array value for $options: 'Profile & Settings'=>'Profile & Settings',

    in the select list, it gets converted to : <option value="Profile &amp; Settings" label="Profile & Settings">Profile &amp; Settings</option>

    and this value gets inserted into the database: Profile &amp; Settings

    When I return to edit the entry again, it doesn't have Profile & Settings selected. Instead, it defaults back to the first option in the select list. In fact, the select list goes back to the first option as soon as I click save since the page reloads.
  • edited 5:34AM
    OK... I'll look into that issue.
Sign In or Register to comment.