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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

[SOLVED] Set CheckBoxSetField to defaul to unchecked


Go to End


5 Posts   1116 Views

Avatar
Optic Blaze

Community Member, 190 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, 187 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, 190 Posts

19 February 2014 at 4:15am

3-12

Avatar
thomas.paulson

Community Member, 107 Posts

19 February 2014 at 3:08pm

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

Avatar
Optic Blaze

Community Member, 190 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'),