Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Populating form labels for dynamic optionsetfield


Go to End


2 Posts   1389 Views

Avatar
Harley

Community Member, 165 Posts

2 December 2010 at 2:41am

Hi,

I've got this code for a dynamic optionsetfield:

if(Dataobject::get("myFourthStep")){
$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?

Thanks

Avatar
Hamish

Community Member, 712 Posts

2 December 2010 at 12:02pm

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');