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:

Customising the CMS /

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

Customizing dropdownfield


Go to End


7 Posts   4278 Views

Avatar
joelg

Community Member, 134 Posts

4 July 2009 at 6:20pm

Hi everyone

I'm making a simple dropdown in the CMS based on Dataobject::get, but how can I push a default value into the dropdownmap?

This is how my dropdown looks now:

DataobjectResult1
DataobjectResult2
DataobjectResult3
etc...

But I would like to push in a default value in the beginning, like so:

None choosen
DataobjectResult1
DataobjectResult2
DataobjectResult3
etc...

Here's my code by the way:


$zoneDropdown = Dataobject::get("ItalyZonePage", "ParentID = $this->ParentID")->toDropdownMap("ID", "Title");
	 
	  
$fields->addFieldToTab("Root.Content.Main", new DropdownField(
	    	'ZoneID',
	    	'Vælg Zone',
	    	$zoneDropdown
	    	));

Hope someone can help, thanks!

Joel

Avatar
SilverRay

Community Member, 167 Posts

4 July 2009 at 7:00pm

If I understand you correctly, it would be the fourth argument:

$fields->addFieldToTab("Root.Content.Main", new DropdownField(
		'ZoneID',
		'Vælg Zone',
		$zoneDropdown,
		$value
       ));

See:
http://api.silverstripe.com/forms/fields-basic/DropdownField.html

HTH

Avatar
joelg

Community Member, 134 Posts

4 July 2009 at 8:06pm

Hi SilverRay

Well, not really. I want to push a new value into the array. The parameter you think of only determines which of the existing values will be default.

Any ideas?

Joel

Avatar
SilverRay

Community Member, 167 Posts

4 July 2009 at 8:35pm

Ah, I see what you mean. Perhaps you can push something into a new DataObjectSet, for an example see:

http://doc.silverstripe.com/doku.php?id=tutorial:3-forms

Scroll to where they talk about the function BrowserPollResults, so basically you would make a new one with ItalyZonePage's and then add something to it? If this doesn't do it, I wouldn't know...

Avatar
Willr

Forum Moderator, 5523 Posts

4 July 2009 at 9:31pm

I'm pretty sure the constructor allows you to specify a default null value and you can use that. It is the 6th parameter

DropdownField('SomeField', 'Some Field', $array, "DefaultValue", null, "Empty Value Title");

If you want to add a dataobject onto a dataobject set you can use $dataobject->push($object) to add a new one to the set but this is only if you want None choosen to be an object, really method #1 would work best and it would make the database value 'null'

Avatar
joelg

Community Member, 134 Posts

5 July 2009 at 8:54am

Hi Willr and SilverRay

Thanks both for your help. Your solution, Willr, did the job, so many thanks.

Joel

Avatar
joelg

Community Member, 134 Posts

5 July 2009 at 6:23pm

Ok, a little question more.

How come, if the default is selected from the dropdown, I get an error, when I try to save the page in the CMS?