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

How do I disable some checkboxes but not others?


Reply

6 Posts   1868 Views

Avatar
david_nash

17 September 2009 at 6:09pm Community Member, 55 Posts

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?

Avatar
banal

17 September 2009 at 6:24pm Community Member, 901 Posts

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?

Avatar
david_nash

17 September 2009 at 6:43pm Community Member, 55 Posts

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!

Avatar
banal

17 September 2009 at 7:26pm (Last edited: 17 September 2009 7:32pm), Community Member, 901 Posts

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.

Avatar
david_nash

18 September 2009 at 12:55pm Community Member, 55 Posts

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?

Avatar
banal

18 September 2009 at 6:21pm Community Member, 901 Posts

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