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.

Data Model Questions /

[SOLVED] Set CheckBoxSetField to defaul to unchecked


Reply


5 Posts   458 Views

Avatar
Optic Blaze

Community Member, 162 Posts

19 February 2014 at 12:15am

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')   
         )........

Avatar
kinglozzer

Community Member, 151 Posts

19 February 2014 at 4:08am

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

Avatar
Optic Blaze

Community Member, 162 Posts

19 February 2014 at 4:15am

3-12

Avatar
thomas.paulson

Community Member, 80 Posts

19 February 2014 at 3:08pm

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

Avatar
Optic Blaze

Community Member, 162 Posts

19 February 2014 at 8:05pm

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