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.

Form Questions /

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

Setting defaults in CheckboxSetField


Go to End


3 Posts   1451 Views

Avatar
jayslippy

Community Member, 10 Posts

10 January 2012 at 5:58am

I've got a form that's part of an 'Edit Profile' type page where the user makes a check box style selection from a list of Capabilities. I'm having problems figuring out what to pass as the last value in the constructor to set the defaults though. Obviously since it's an edit profile page I need the users current selections to show up when they load the page.

Code so far:

$capabilities = DataObject::get('Capability');
$capabilityField = new CheckboxSetField(
'Name',
'Capabilities: ',
$capabilities,
Array(
$testcap->Name
)
);

I have tried lots of different formats for the last parameter, including an array of Capability names, an array that looks like 'engines' => '1', an array of integers. What is the correct format to pass for the default array so that I can control which of the checkboxes are ticked?

Any advice appreciated.

Thanks

Avatar
jayslippy

Community Member, 10 Posts

14 January 2012 at 2:14am

I eventually gave up on this one, and used a series of optionsetfields instead. It was quite easy that way to set the values to '1' or '0'.

Now though, I'm running into the same problem with a DropdownField. I'm trying to set the selected value of the field when the form is loaded but everything I try to set as a default value (eg, '4', 4, $capabilityMap[4], $capabilityMap['4'], Capability objects, titles of capability objects) has zero effect.

Can somebody please point me in the direction of the correct syntax for the $value parameter to get this to work? There are quite a few web pages describing the use of DropdownField, but very few show the $value functionality in use.

$capabilities = DataObject::get('Capability');
$capabilityMap = $capabilities->toDropDownMap('ID','Title');
$capabilityField = new DropdownField(
'capability',
'Main capability requirement',
$capabilityMap,
'4'
);

Avatar
jayslippy

Community Member, 10 Posts

14 January 2012 at 2:48am

Resolved => code was working but was being overwritten by loading data into the form.