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

Checking checkboxes in a templated form


Go to End


2 Posts   1487 Views

Avatar
NETim

Community Member, 24 Posts

13 February 2013 at 5:26am

Hi,

Working on a site that has a search filter bar with checkboxes.

Try as i might i haven't been able to check the selected checkboxes when the page loads.

The basic idea is that a search page can be set to pre-select certain items (not all). The page will also check any checkboxes chosen by the user when the form reloads.

Any suggestions of how to do this?

Cheers,

T.

Avatar
Willr

Forum Moderator, 5523 Posts

16 February 2013 at 10:26pm

If you're using a CheckboxSetField you should be able to call setValue() on the field and pass in your array of defaults. As long as the keys match, they will be selected by default

$field = new CheckboxSetField('Field', 'Title', array('Option1' => 'Foo', 'Option2' => 'Bar'));
$field->setValue(array('Foo' => true));