Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Set default value for OptionsetField

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

Page: 1
Go to End
Author Topic: 2486 Views
  • Ben_W
    Avatar
    Community Member
    80 Posts

    Set default value for OptionsetField Link to this post

    I have the following code in one of my forms for the backend cms, it seems that $source will only take numeric array, not associative array.
    original code:
    $myMediaType = new OptionsetField(
                       $name = "MediaType",
                       $title = "Media Type",
                       $source = array(
                          "Video" => "Video",
                          "Audio" => "Audio"
                       ),
                       $value = "Video"
                      );
          $fields->addFieldToTab('Root.Content.Main', $myMediaType, 'Content');   

    this will not work, but if I change the $source to a numeric array, it will set the default value.

    $myMediaType = new OptionsetField(
                       $name = "MediaType",
                       $title = "Media Type",
                       $source = array("Video", "Audio"),
                       $value = 0
                      );
          $fields->addFieldToTab('Root.Content.Main', $myMediaType, 'Content');

    Does anyone come cross this problem, I would prefer to store string 'Video', 'Audio' into the database rather than integer. Could someone please point me the right direction? Thank you!

  • mikhail
    Avatar
    Community Member
    4 Posts

    Re: Set default value for OptionsetField Link to this post

    Old thread, but I'm having the same problem. Resorted to storing int and using a numeric array as source.
    Would still prefer to store a string.

    If using associative array, on first load the OptionsetField marks no default. On page refresh, it correctly marks the db value.

    ...Class PieHolder...

    public static $db = array(
    'Pie' => "Enum('Pie1, Pie2, Pie3')"
    )

    ...Form Constructor...

    new OptionsetField('Pie', 'Pie', $PieHolder->dbObject('Pie')->enumValues(), $PieHolder->Pie);

    === no default marked on first load

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