Jump to:

5554 Posts in 1742 Topics by 1227 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Customizing dropdownfield

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

Page: 1
Go to End
Author Topic: 3581 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    Customizing dropdownfield Link to this post

    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

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Customizing dropdownfield Link to this post

    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

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: Customizing dropdownfield Link to this post

    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

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Customizing dropdownfield Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Customizing dropdownfield Link to this post

    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'

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: Customizing dropdownfield Link to this post

    Hi Willr and SilverRay

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

    Joel

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: Customizing dropdownfield Link to this post

    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?

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