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.

Form Questions

Setting defaults in CheckboxSetField


Reply

3 Posts   596 Views

Avatar
jayslippy

10 January 2012 at 5:58am Community Member, 10 Posts

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

14 January 2012 at 2:14am Community Member, 10 Posts

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

14 January 2012 at 2:48am Community Member, 10 Posts

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