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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Dropdownfield doesn't set selected item


Reply


4 Posts   408 Views

Avatar
quanto

Community Member, 81 Posts

22 March 2013 at 1:29am

Edited: 22/03/2013 1:29am

Hi,

I'm trying to insert a DropDownfield into a CMSfield popup in Silverstripe 2.4.

The right value is inserted into the DB after saving. However, the Dropdownfield doesn't stay to the selected item but returns to the first. My code:

class Link extends DataObject{
static $db = array(
'Descript'=> 'Text',
'ExternURL'=> 'Text',
'InternURL'=> 'Varchar'

);

static $has_one = array(
'Pagina' => 'SiteTree'
);

public function getCMSFields_forPopup() {
$fields = parent::getCMSFields();
$fields = new FieldSet(
new TextField('Descript', 'Description'),
new TextField('ExternURL', 'External link (including http://)'),
new DropdownField('InternURL','OR Internal link:', DataObject::get("SiteTree")->toDropdownMap('ID', 'Title', '(Select one)', true))
);

return $fields;
}
}

Anyone a solution?

Avatar
quanto

Community Member, 81 Posts

23 March 2013 at 2:32am

Found the solution: I got a function "getInternURL()", but that's not possible.

Avatar
Willr

Forum Moderator, 5513 Posts

26 March 2013 at 8:10pm

InternURL, your managed DropdownField should rather be in your has_one array if you want to store a relationship between Link and a SiteTree object.

static $has_one = array(
'Pagina' => 'SiteTree',
'InternURL' => 'SiteTree'
);

new DropdownField('InternURLID','OR Internal link:', DataObject::get("SiteTree")->map('ID', 'Title')))

Avatar
quanto

Community Member, 81 Posts

28 March 2013 at 2:55am

Thanks, it works for me! However, it took me an hour to find out the ID after the InternURL.

Even easier now:

$fields->addFieldToTab('Root.Main', new TreeDropdownField('InternURL', 'Link Blok 2', 'SiteTree'));