1505 Posts in 474 Topics by 460 members
Form Questions
SilverStripe Forums » Form Questions » How do I disable some checkboxes but not others?
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 1285 Views |
-
How do I disable some checkboxes but not others?

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?
-
Re: How do I disable some checkboxes but not others?

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? -
Re: How do I disable some checkboxes but not others?

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! -
Re: How do I disable some checkboxes but not others?

17 September 2009 at 7:26pm Last edited: 17 September 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.
-
Re: How do I disable some checkboxes but not others?

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?
-
Re: How do I disable some checkboxes but not others?

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..
| 1285 Views | ||
|
Page:
1
|
Go to Top |


