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

How do I disable some checkboxes but not others?


Go to End


6 Posts   3212 Views

Avatar
david_nash

Community Member, 55 Posts

17 September 2009 at 6:09pm

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
bummzack

Community Member, 904 Posts

17 September 2009 at 6:24pm

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

Community Member, 55 Posts

17 September 2009 at 6:43pm

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
bummzack

Community Member, 904 Posts

17 September 2009 at 7:26pm

Edited: 17/09/2009 7:32pm

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

Community Member, 55 Posts

18 September 2009 at 12:55pm

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
bummzack

Community Member, 904 Posts

18 September 2009 at 6:21pm

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