Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Set CheckBoxSetField to defaul to unchecked

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

Page: 1
Go to End
Author Topic: 343 Views
  • Optic Blaze
    Avatar
    Community Member
    161 Posts

    [SOLVED] Set CheckBoxSetField to defaul to unchecked Link to this post

    Hi there,

    I have a CheckboxSetField that is populated with an array. The first item in the array is checked by default, but i dont want any items checked by default. Code looks like this:

    public function LeakForm() {
    $areas = array('Exterior','Water Meter (Moving)','Water Meter (Not Moving)','Roof Space','Family Bathroom','En-Suite
       $form = Form::create(
    $this,
    "LeakForm",
    FieldList::create(
                CheckboxSetField::create("AreasTested", "Area Tested", $areas)
                   ->defaultValue('No')   
             )........

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: [SOLVED] Set CheckBoxSetField to defaul to unchecked Link to this post

    Which version of SilverStripe are you using? You should be able to do this in 3.1 with ->setDefaultItems(array()); or perhaps ->setValue(false); - http://api.silverstripe.org/3.1/class-CheckboxSetField.html

    Loz

  • Optic Blaze
    Avatar
    Community Member
    161 Posts
  • thomas.paulson
    Avatar
    Community Member
    67 Posts

    Re: [SOLVED] Set CheckBoxSetField to defaul to unchecked Link to this post

    try CheckboxSetField::create("AreasTested", "Area Tested", $areas)->setValue('No')

  • Optic Blaze
    Avatar
    Community Member
    161 Posts

    Re: [SOLVED] Set CheckBoxSetField to defaul to unchecked Link to this post

    Ok figured it out... the array had to have a key as well so it should look like this:
    $areas = array(
             'Exterior'=>'Exterior',
             'Water Meter (Moving)'=>'Water Meter (Moving)',
             'Water Meter (Not Moving)'=>'Water Meter (Not Moving)',
             'Roof Space'=>'Roof Space',
             'Family Bathroom'=>'Family Bathroom',
             'En-Suite Bathroom'=>'En-Suite Bathroom',
             'Kitchen'=>'Kitchen',
             'Passage'=>'Passage',
             'Bedroom'=>'Bedroom'
          );

    And then you can set the fieldlist (like thomas suggested) like this:

    CheckboxSetField::create("AreasTested", "Area Tested", $areas)->setValue('0'),

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