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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

[SOLVED] Set CheckBoxSetField to defaul to unchecked


Go to End


5 Posts   1425 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'),