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.

Form Questions /

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

Set default value on dynamic DropdownField


Go to End


4 Posts   7110 Views

Avatar
Carbon Crayon

Community Member, 598 Posts

7 February 2009 at 2:13am

Hi Guys

I am trying to set the default value of a dropdown which is generated from the ->toDropdownMap function, but it doesnt seem to work. Here is my code:

new DropdownField('Service', 'Service', Dataobject::get("ServicePage")->toDropdownMap("ID", "Title"), $value=15)

The default just remains as the first Item in the list. I noticed the isSelected() function in the api docs, but I'm not sure where to call that if it's what I need?

Any help is much appreciated :)

Avatar
Carbon Crayon

Community Member, 598 Posts

7 February 2009 at 6:52am

OK, I'm being a muppet, this does work, but doesn't change when you hit refresh, gotta load the page by revisiting the URL....

Avatar
UncleCheese

Forum Moderator, 4102 Posts

7 February 2009 at 7:18am

Aram,

Where is the value of $value coming from?

Avatar
Carbon Crayon

Community Member, 598 Posts

7 February 2009 at 7:34am

Edited: 07/02/2009 7:36am

it being passed into the page via the URL, it's the ID of the page the user clicked through to get to the form.

It's for a quote page, each service provided by the client has a page with a get quote button, so if you click it then it takes you to the quote form which has the service type drop down pre-populated.