1

Because of this major Drupal 7 bug (http://drupal.org/node/811542), I cannot use radio options in my form.

I have three options in my form and I want them to be checkboxes, but the user can only select one checkbox. By Drupal 7's auto form design, it converts single value items to become radios. How can I get the below to become checkboxes that users can only select one without having to write my own module to override the form?

beef|I prefer beef

chicken|I prefer chicken

veggie|I am a vegetarian

1
  • 1
    The core issue has been fixed.
    – colan
    Nov 26, 2013 at 21:40

3 Answers 3

3

Simply create three fields. One for each of your beef, checken and veggie with type: Boolean widget: Single on/off checkbox

The use the Conditional Fields module to create a relationship between the three, so that when one is checked off the other two disappear.

0
3

Since checkboxes are multi-value options by nature. It would be detrimental to usability to override this default and expected behavior. You could change the validation as jimajamma suggests, but this would definitely lead to mass confusion amongst your users, as checkboxes are specifically intended for use for multi-value selections.

A slightly better, but still sub-optimal, option would be to use javascript to make sure that only one checkbox option is selected. Using javascript would at least mean that users have immediate feedback rather than finding out after submitting the form (and it failing validation) that the completely expected behavior of multiple values for checkbox form items is no acceptable in this situation.

Since the Drupal core issue that you linked to specifically refers to sets of radio options that are not set as required, why not re-phrase the radio options so that you can make the form item be required?

For example, instead of the three options

beef|I prefer beef
chicken|I prefer chicken
veggie|I am a vegetarian

create a forth option,

none|No preference
beef|I prefer beef
chicken|I prefer chicken
veggie|I am a vegetarian

and set that fourth option as the default. In this way, you can successfully work-around the existing Drupal bug without confusing your users with non-standard behavior of form items and without creating any additional work for them. Not only is this better for users, but it requires no code customizations and is more easily maintainable (and easier to change once the bug is fixed).

In a situation with a yes/no question, you could use checkboxes and only provide one option, "Yes." Then you could evaluate "Yes" in its unchecked state the same way you would have evaluated "No" in its checked state.

8
  • I see. So you are suggesting that I reframe the question to a simple yes/no to utilize a single checkbox. I suppose this would do the trick.
    – user785179
    Feb 9, 2012 at 6:59
  • I chose the simple yes/no as an easy example, and I should not have done that (my bad). I updated my original question to have 3 choices. Is there any other way I can get the same effect as radios? I realize I could use a select list, but I have other questions that are longer sentences that end up taking a lot of room in terms of width. Select options don't seem to wrap.
    – user785179
    Feb 9, 2012 at 7:09
  • I edited my answer to include a similar language-based, rather than programming-based, solution for situations other than yes/no options.
    – sheena_d
    Feb 9, 2012 at 14:57
  • Sorry to add complexity to this, but what if "none|No preference" was not an option? You either eat chicken, beef, or are a vegetarian (I know this example is getting lame, but just go with it, please). The only simple way to remove the default "N/A" from not required radios choices would be to make it required, correct? This would put me back into the Drupal bug problem. Or is there a way to easily remove "N/A"? Anyhow, I think the best option is if Drupal fixes this bug (since 2010!) or I override the form with my own custom module. I appreciate your response and help.
    – user785179
    Feb 9, 2012 at 18:46
  • 1
    Let me make sure I understand the situation correctly. You want to have a required field that allows only one value, but does not return the confusing error message "An illegal choice has been detected. Please contact the site administrator." And you are concerned that my Answer will somehow create duplicate "None" options. Is that correct? Also, I have thus far assumed that the form you are building is a content type via the Drupal UI, but please let us know how you are actually building this form (Webform module? FAPI in a custom module?).
    – sheena_d
    Feb 9, 2012 at 23:10
0

You could also allow for multiple values to force the checkboxes and then create a custom validation function that makes sure only one is actually checked.

2
  • But this validation would require me to write a custom module, correct?
    – user785179
    Feb 9, 2012 at 7:05
  • it would require custom php code yes
    – Jimajamma
    Feb 9, 2012 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.