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

Setting CheckboxField as checked on handmade form


Go to End


2 Posts   1529 Views

Avatar
jbothma

Community Member, 5 Posts

9 May 2012 at 10:56am

Edited: 09/05/2012 11:02am

Hi

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.

e.g.

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

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 http://api.silverstripe.org/trunk/forms/core/FormField.html#methodsetAttribute 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?

Avatar
jbothma

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?