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

Default selected value - OptionsetField


Go to End


2 Posts   4672 Views

Avatar
quamsta

Community Member, 29 Posts

10 January 2009 at 8:53am

Hi, I'm having some issues changing the default selected value when I make an OptionsetField in Page.php for use inside SilverStripe.

Here is my code:

	new OptionsetField(
		  $name = 'Options',
		  $title = 'Options',
		  $source = singleton('Page')->dbObject('Options')->enumValues(),
		  $value = "2"
		   )

However, no matter what I set "$value" to, whenever I create a new page in the CMS, the first option is always selected. Does anyone know why this is the case?

Avatar
tobych

Community Member, 97 Posts

19 February 2009 at 4:06pm

I don't really know what the code for the $source is doing, but the documentation is here - http://doc.silverstripe.com/doku.php?id=optionsetfield - and note that $value refers to the index used in the $source array, not the number of the element in that array. Sorry, language is failing me here, but if your $source is

2 => "Apple"
54 => "Orange"
512 => "Banana"

then $value="2" will mean the default will be "Apple".