Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Setting defaults in CheckboxSetField

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

Page: 1
Go to End
Author Topic: 485 Views
  • jayslippy
    Avatar
    Community Member
    10 Posts

    Setting defaults in CheckboxSetField Link to this post

    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

  • jayslippy
    Avatar
    Community Member
    10 Posts

    Re: Setting defaults in CheckboxSetField Link to this post

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

  • jayslippy
    Avatar
    Community Member
    10 Posts

    Re: Setting defaults in CheckboxSetField Link to this post

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

    485 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.