Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to use a text field to create a select list (both in beckend)?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 678 Views
  • theGermaniac
    Avatar
    Community Member
    82 Posts

    How to use a text field to create a select list (both in beckend)? Link to this post

    Hi,

    I want to create a select list with data taken from a text field. The text field is listed in the backend and so is the select list.

    The text field should be something like "Value 1,Value XY, Another Value" (separated by comma) and is located at the page holder. The pages underneath this holder should show the select list with the values taken from the before mentioned text field.

    With what I don't have problems is creating the single elements. My problem ist to parse the text field values to the select list.

    Any help appreciated!

    Cheers, Mario

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: How to use a text field to create a select list (both in beckend)? Link to this post

    http://php.net/manual/de/function.explode.php

    You should also consider using a simple DataObject for this, so if anyone deletes values in the TextField your keys won't get mixed up.

    Something like:

    class SelectModelAdmin extends ModelAdmin {
       public static $managed_models = array(
          'SelectModel'
       );
       static $url_segment = 'selectadmin';
       static $menu_title = 'Select Admin';
    }
    class SelectModel extends DataObject {
       static $db = array('Name' => 'Text');
       static $searchable_fields = array('Name');
    }
    class Page extends SiteTree {
       static $has_one = array(
          'Select' => 'SelectModel',
       );
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $select = new DropdownField('SelectID', 'Select', DataObject::get('SelectModel')->map('ID','Name'));
          $fields->addFieldToTab('Root.Content.Main', $select, 'Content');
          return $fields;
       }
    }

    678 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.