Jump to:

23480 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » [Solved] Display the values of a CheckboxSetField in a template?

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

    [Solved] Display the values of a CheckboxSetField in a template? Link to this post

    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 %>
    $Topics
    <% end_if %>

    returns a blank

    and
    <% control Topics %>
    <ul>
    <% 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 %>
    </ul>
    <% end_control %>

    Returns
    - 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',
    );

    and

    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;
    }
    }

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: [Solved] Display the values of a CheckboxSetField in a template? Link to this post

    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.

  • tonito
    Avatar
    Community Member
    24 Posts

    Re: [Solved] Display the values of a CheckboxSetField in a template? Link to this post

    Thank you! This is what I needed.

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