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.

General Questions

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

[Solved] Display the values of a CheckboxSetField in a template?


3 Posts   1092 Views


24 August 2010 at 3:54pm (Last edited: 29 August 2010 2:27pm), Community Member, 24 Posts

Can anyone please help? I created a CheckboxSetField following the documentation, but I couldn't figure out in neither the forum or documentation how to display the result:

$Topics returns 1, 2 (etc)

<% control Topics %>
<% end_if %>

returns a blank

<% control Topics %>
<% if 1 %><li>Technology</li><% end_if %>
<% if 2 %><li>Gardening</li><% end_if %>
<% if 3 %><li>Cooking</li><% end_if %>
<% if 4 %><li>Sports</li><% end_if %>
<% end_control %>

- Technology
- Gardening
- Cooking
- Sports

whatever is checked.

Do I need to write a controller?

Here the code that I used:

static $db = array(
'Topics' => 'Varchar',


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

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;


24 August 2010 at 8:12pm Forum Moderator, 5511 Posts

Because you have stored the value as a varchar it stores it as a string (as per your output). I thought it exploded this automatically (so you could control over it) but it must not. Therefore you will need a function like this to be able to iterate over the values. Put this in the same object you have the $db

function ExplodedTopics() {
      $set = new DataObjectSet();
      if($topics = $this->Topics) {
         foreach(explode(',',$topics) as $key => $value) {
            $set->push(new ArrayData(array('Value' => $value)));

return $set;

That will give you an object which you can iterate over like <% control ExplodedTopics %> $Value <% end_control %>. By doing this also you could also move all the <% if 1.. %> logic into the PHP. Eg store the mapping of value = topic (if its not already) in the code so you can reuse it.


29 August 2010 at 2:27pm Community Member, 24 Posts

Thank you! This is what I needed.