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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

Newbie Question: CheckboxSetField


Go to End


3 Posts   2956 Views

Avatar
tonito

Community Member, 24 Posts

28 June 2010 at 4:00pm

Edited: 24/08/2010 3:11am

<code>
class Test extends Page {
static $db = array(
);
static $has_one = array(
);

function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldsToTab(
'Root.Content.Main',
array(
new CheckboxSetField(
$name = "topics",
$title = "I am interested in the following topics",
$source = array(
"1" => "Technology",
"2" => "Gardening",
"3" => "Cooking",
"4" => "Sports"
),
$value = "1"
)
)
);

return $fields;
}

}
</code>

I realize this is a very basic question, but I am new to silverstripe and I am stumped.

How do I declare the variable "topics", as it stands, the checkboxes appear fine in the admin part, but I can't save them (they always revert back to the default value) nor display results.

Avatar
Jedateach

Forum Moderator, 238 Posts

28 June 2010 at 6:40pm

Edited: 28/06/2010 6:41pm

Hi tonito,
Try adding a database field to save the selection into. Your $db array becomes:

static $db = array(
'topics' => 'Varchar'
);

Because your checkboxsetfield also has the name 'topics' , the data will be saved into the 'topics' database field for each test page.

Chances are you'll be wanting to do something more advanced than that. You should learn about the SilverStripe datamodel , perhaps also go through the tutorials.

Avatar
tonito

Community Member, 24 Posts

23 August 2010 at 11:50am

Edited: 24/08/2010 3:35pm

Thank you it's what I needed.