Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Dropdownfield doesn't set selected item

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

Page: 1
Go to End
Author Topic: 339 Views
  • quanto
    Avatar
    Community Member
    81 Posts

    Dropdownfield doesn't set selected item Link to this post

    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?

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: Dropdownfield doesn't set selected item Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Dropdownfield doesn't set selected item Link to this post

    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')))

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: Dropdownfield doesn't set selected item Link to this post

    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'));

    339 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.