Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Setting CheckboxField as checked on handmade form

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

Page: 1
Go to End
Author Topic: 611 Views
  • jbothma
    Avatar
    Community Member
    5 Posts

    Setting CheckboxField as checked on handmade form Link to this post

    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?

  • jbothma
    Avatar
    Community Member
    5 Posts

    Re: Setting CheckboxField as checked on handmade form Link to this post

    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?

    611 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.