Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to use an OptionsetField ?


Go to End


3 Posts   602 Views

Avatar
jaaf

Community Member, 24 Posts

17 February 2014 at 7:51am

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 ?

Avatar
kinglozzer

Community Member, 187 Posts

17 February 2014 at 8:43am

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

Avatar
jaaf

Community Member, 24 Posts

17 February 2014 at 7:57pm

Thank you kinglozzer,

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