Jump to:

5521 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Dropdown ignores database entry

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

Page: 1
Go to End
Author Topic: 854 Views
  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Dropdown ignores database entry Link to this post

    I know it is not only me having this problem, but no one can tell me how to fix this. I have a dropdownfield and want to save an id with it. After submit, the id is stored in the database and the dropdown shows the selected value. But after requesting the form from the sitetree again, the dropdown shows the first value, in my case "Please choose".

    Example of my source (Location extends DataObject):

       public static $has_one = array(
          "MeetingPoint" => "Location"
       );

       public function getCMSFields() {
        $fields = parent::getCMSFields();
       
        // create or find tabs
        $fields->findOrMakeTab( "Root.Content.Locations" );

    $meetingPoint = new DropdownField( "MeetingPoint", "Meeting Point", DataObject::get( "Location" )->toDropDownMap( "ID", "Name", "Please choose" ) );
          $fields->addFieldToTab( "Root.Content.Locations", $meetingPoint );
        return $fields;
       }

       function onBeforeWrite()
       {
          $this->MeetingPointID = $this->MeetingPoint;
          parent::onBeforeWrite();
       }

    Is this an official bug?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Dropdown ignores database entry Link to this post

    You should name your dropdown field 'MeetingPointID' rather then 'MeetingPoint' and then get rid of your onBeforeWrite() call. Its confusing SS since when it loads the data it doesn't quite remember that MeetingPointID is to go into MeetingPoint. Well at least I'm pretty sure thats the solution.

  • eddieconnecti
    Avatar
    Community Member
    26 Posts

    Re: Dropdown ignores database entry Link to this post

    Great! This fixed it! I can now use all my dropdowns and it selects stored values as given! Perfect! Thanks a lot!

    854 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.