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.

Form Questions

Setting CheckboxField as checked on handmade form


Reply

2 Posts   623 Views

Avatar
jbothma

9 May 2012 at 10:56am (Last edited: 9 May 2012 11:02am), Community Member, 5 Posts

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

11 May 2012 at 7:29am Community Member, 5 Posts

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?