Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » How do I disable some checkboxes but not others?

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

Page: 1
Go to End
Author Topic: 1830 Views
  • david_nash
    Avatar
    Community Member
    55 Posts

    How do I disable some checkboxes but not others? Link to this post

    How do I create a set of checkboxes, all with the same name, but some disabled and some not?

    If I use CheckBoxSetField->setDisabled($value) it disables every checkbox.

    I also tried creating several CheckBoxFields with the same name, but it only displays one.

    What should I do?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How do I disable some checkboxes but not others? Link to this post

    Hi

    What does "same name" mean? That they all have the same label in the form, or that they have the same name? If you're trying to do the latter, please explain why, because using the same name for different form-fields usually breaks the functionality of a form (only one value is returned).
    Also: Do you really mean disabled, or unchecked?

  • david_nash
    Avatar
    Community Member
    55 Posts

    Re: How do I disable some checkboxes but not others? Link to this post

    I mean the name attribute is the same for all of them, but they have different values and different labels

    Eg I want to end up with something like this markup

    <input type="checkbox" name="count" value="1"> One
    <input type="checkbox" name="count" value="2" disabled="disabled"> Two
    <input type="checkbox" name="count" value="3"> Three


    thanks!

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How do I disable some checkboxes but not others? Link to this post

    Ok, I see. I guess that isn't supported with the CheckboxSetField, since it uses the name as key in an array structure. Therefore duplicate keys aren't possible (existing keys will be overwritten). Your best bet is to write some custom code that creates a bunch of CheckboxFields with the desired properties. You should also use count[] as name, instead of count. That will allow you to access the values as array on the server-side (in php). Read here for further information: http://www.bestcodingpractices.com/php_multiple_values_in_one_form_variable-30.html

    Update The CheckboxSetField actually uses the "array" syntax mentioned above. Field names will be setname[fieldkey]. But there's no way to disable single elements. You could extend the CheckboxSetField class and add the required functionality though.

  • david_nash
    Avatar
    Community Member
    55 Posts

    Re: How do I disable some checkboxes but not others? Link to this post

    Thanks banal, it's good to know that it's the system, not me.

    I think it would be easier to just forego the SS forms and write some HTML, wouldn't it? This isn't a huge project and I'd like to get it done quickly. Your thoughts?

    How would I go about extending CheckboxSetField?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How do I disable some checkboxes but not others? Link to this post

    I would probably create a new class, say AdvancedCheckboxSetField that extends CheckboxSetField. Then I would implement some methods that allow me to enable/disable a checkbox by index, eg. enableCheckbox($index) and disableCheckbox($index). The enabled status will simply be stored in an array. After that, all that's left to do is to override the Field method. There's pretty much code in the CheckboxSetField::Field method, but most of that can probably be copied over to the new class. Instead of using the global "disabled" flag, you can then check your "is-enabled-array" for each index individually and write html markup accordingly.

    You can place your new class in mypage/code/forms and also reuse it for other projects..

    1830 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.