Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » How to use an OptionsetField ?

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: 209 Views
  • jaaf
    Avatar
    Community Member
    24 Posts

    How to use an OptionsetField ? Link to this post

    Hi,
    I am alway relatively new to SilverStripe and not very used to PHP programming.

    I would like to do an ArticleHolder whose behaviour depend on a var (here MyType)
    I did the following in the DataObject

    <?php
    class ArticleHolder extends Page {

    private static $allowed_children = array('ArticlePage');
    private static $db=array(
       'MyType' => "Varchar(10)"
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
       $typeField = new OptionsetField(
          $name = "MyTypeID",
          $title = "Type de conteneur",
          $source = array(
          "1" => "Option 1",
          "2" => "Option 2",
          "3" => "Option 3",
          "4" => "Option 4",
          "5" => "Option 5"
          )

          );
    $fields->addFieldToTab('Root.Main',$typeField,'Content');
    return $fields;
    }

    }
    class ArticleHolder_Controller extends Page_Controller {
    }


    Apparently, in the cms I have the right option set, but when I select a value and register, the selection is lost.
    What should I do in order for my ArticleHolder to memorize the user's choice ?

  • kinglozzer
    Avatar
    Community Member
    140 Posts

    Re: How to use an OptionsetField ? Link to this post

    Hi jaaf,

    Your database field is called 'MyType', but your field name is 'MyTypeID'. You only need to append 'ID' if you're referring to a has_one relation, so you should rename your field (the first, $name parameter) to 'MyType'.

    Loz

  • jaaf
    Avatar
    Community Member
    24 Posts

    Re: How to use an OptionsetField ? Link to this post

    Thank you kinglozzer,

    I see now. Renaming of field was not necessary here.

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