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

Neater way of template for CheckboxSetField form item


Go to End


1861 Views

Avatar
MarijnKampf

Community Member, 176 Posts

28 July 2009 at 3:54am

I'm now using the following code to display the selected checkboxes from a CheckboxSetField in a form:

MyForm.php

new CheckboxSetField(
	$name = "MyCheckboxes",
	$title = "Title",
	$source = array(
		"Option1" => "Option 1",
		"Option2" => "Option 2",
	)
)

MyForm.ss

<% control MyCheckboxes %>
	<% if Option1 %><li>One</li><% end_if %>
	<% if Option2 %><li>Two</li><% end_if %>
<% end_control %>

Is there an easier/better way of only including the selected checkboxes in my form? As the above works well with a small number of options, but quickly adds a lot of code for longer option lists.