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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

*SOLVED* Default static value in dynamically populated DropdownField

Go to End

3 Posts   1248 Views


Community Member, 29 Posts

25 February 2013 at 7:50am

Is it possible to add a default static value to a dynamically populated DropdownField in admin?

In my fieldset I have this code:

new DropdownField('BlogCategory', 'Choose a Category', Dataobject::get("BlogCategory")->map("ID", "Title"))

I tried following the instructions in this article (, but I just keep getting server errors. I suspect it's because the article was written pre SS3 and things have changed now.

Can anyone help?


Community Member, 187 Posts

25 February 2013 at 10:16pm

Make sure you put your site in dev mode: in _config.php add the line


You should then be able to see the errors properly. If you don't see them, try refreshing the page or looking in your javascript console (download Firebug for Firefox if you don't know what that is).

Do you actually have any 'BlogCategory' DataObjects? If not, that would likely cause an error.

Also, you mention adding the DropdownField to the admin area, but the instructions are for adding it to the front-end of the site.


Community Member, 29 Posts

25 February 2013 at 10:49pm

Thanks for the info about setting dev mode in _config.php - I've done it when using _ss-environment.php, but not in config before.

After a bit more digging around (and chatting with a dev friend) I managed to sort it out. There's a function in the DropdownField class called setEmptyString(), which does exactly that.

My code is basically for a 'Latest Posts' widget. I want the user to be able to filter the latest posts by Category (I'm using the blogcategories module: However, I wanted the first value in the dropdown to be 'All Posts'.

For anyone who is interested here's the code I'm using:

public function getCMSFields() {
$categoryDropdown = new DropdownField(
'Choose a Category',
Dataobject::get("BlogCategory")->map('ID', 'Title')
return new FieldList(
new TextField('WidgetTitle', 'Widget Title'),
new NumericField("NumberToShow", "Number to Show"),
$categoryDropdown->setEmptyString('All Categories'),
new TextField('ReadArticleLinkTitle', 'Read Article Link Title')

... And if you look at the attached screen grab you'll see what this looks like in Admin.

Attached Files