Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » Populating form labels for dynamic optionsetfield

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: 946 Views
  • Harley
    Community Member
    153 Posts

    Populating form labels for dynamic optionsetfield Link to this post


    I've got this code for a dynamic optionsetfield:

          $people = Dataobject::get("myFourth");
          $map = $people->toDropDownMap();
          $fields->push( new OptionsetField(
             $name = "PersonSelectedID",
             $title = "Select person",
             $source = $map,
       return $fields;

    I want to use a column in the database to populate the form label, otherwise I just get an automated one which retrieves the ID of the dataobject.

    Any ideas on how I can achieve this?


  • Hamish
    Community Member
    712 Posts

    Re: Populating form labels for dynamic optionsetfield Link to this post

    If you have a look at the API Documentation you'll see that the toDropdownMap method takes the following optional arguments:

    string $index - The field to use as a key for the array
    string $titleField - The field (or method) to get values for the map
    string $emptyString - Empty option text e.g "(Select one)"
    bool $sort - Sort the map alphabetically based on the $titleField value

    So you can do:

    $map = $people->toDropDownMap('ID', 'SomeOtherColumn');

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.