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

String value instead of index from the Dropdownfield


Go to End


2 Posts   1095 Views

Avatar
ville

Community Member, 5 Posts

7 August 2012 at 8:31pm

Hey,

I have a form which contains a dropdownfield in the following format: new DropdownField('Reproducibility', 'Reproducibility', array('Always','Sometimes','Randomly','Have not tried'))

When I'm trying to get the string values, it's giving array index instead of string values. How can I retrieve the real values which are shown in the dropdownfield?

Thanks!

Avatar
jak

Community Member, 46 Posts

7 August 2012 at 11:43pm

Edited: 07/08/2012 11:43pm

The value of the dropdown field is determined by the key in the array you are passing. Since you do not set the keys, they are numeric. Try something like

new DropdownField('Reproducibility', 'Reproducibility', 
  array('always' => 'Always', 'sometimes'=>'Sometimes', 'randomly'=>'Randomly', 'nottried' => 'Have not tried'));

Avoid spaces in the keys.