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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Setting CheckboxField as checked on handmade form

Go to End

2 Posts   1209 Views


Community Member, 5 Posts

9 May 2012 at 10:56am

Edited: 09/05/2012 11:02am


I have a form that's generated by making a FieldSet and adding a new CheckboxField to it, among other inputs, in a particular action function in one of my controllers.

Edit: I want to set a checkbox as checked when the property it represents is true, and unchecked otherwise.


class RadioPage_Controller extends Controller {
    public function getConfigForm() {
        $radio = ...
        $fields = new FieldSet();
        $bassBoostCheckboxField = $radio->getFrontendFields()->fieldByName('BassBoost');

The form is used to edit properties of an object, and their values are filled in when the form is generated (e.g. name, color, etc not shown)

Setting the value for the CheckboxField sets its value, but doesn't result in it being checked when shown in the browser.

$bassBoostCheckboxField->setAttribute('checked', 'checked')
gives some error, I can't remember what now. The CAUTION in doesn't seem like it ought to apply since it's not mentioned and it's only one form field.

This doesn't work if I just create a brand new CheckboxField using its constructor either.

How can I conditionally set a CheckboxField as checked? Is the way I'm making and showing my form the "wrong way" when it represents a subset of the fields of an object?


Community Member, 5 Posts

11 May 2012 at 7:29am

So this can be achieved with a LiteralField, but is this really not possible directly with a CheckboxField?

I'd supply a patch for this if I have time one day - would you be interested or is there a reason why this isn't there?