How can I create a simple on/off checkbox in a content type in D7?

Currently I've got it configured as follows:

  • Field type: List (integer)
  • Values:
    0|No
    1|Yes
  • Number of values: 1 (tried "unlimited" as well)

It always shows both the values, while only "Yes" should be displayed.

When I only use 1 key, my Views Filter Criteria notice: No valid values found on filter: [my fieldname].

None of the documentation I've found works (mostly for D6). Personally I think this should be documented in the field description, for it's so frequently used.

Comments

knalstaaf’s picture

I've got it working after all with the following configuration:

  • Field type: List (integer)
  • Values:
    0|Yes
  • Number of values: unlimited

Concerning the No valid values found error in Views: make sure this filter criterion (on/off checkbox) is only applied on the display it should be applied on. Eventhough I remember setting it "only for this display" it did apply it on all displays. So check your other displays if this occurs...

batigolix’s picture

Thanks knalstaaf, this saves me plenty of time searching for the solution

For completeness I would add the widget type. (And a "kniesoor" like me, would use "1" as the key value for "Yes")

Resulting in:

- Field type: List (integer) & Widget: Check boxes/radio buttons
- Values:
1|Yes
- Number of values: unlimited

knalstaaf’s picture

I'm a bit of a little Pete Precise as well (bless our English), so I'm using "1" too from now on.

Concerning the No valid values found on filter: [my fieldname]-notice, if the problem persists, enable the master display in the settings of Views, and turn off the obsolete field in the master display of your View.

jimmins’s picture

I'm trying to create a checkbox, but it's still showing up as a Select List on the product (add to cart) page. And it has a label of "Select a product" with two products listed. The functionality works, but the label is incorrect and it's not displaying a checkbox. It does show a checkbox on the admin pages when I maintain my products. Any ideas?

Update: It displays a single radio button if I change the Number of Values setting to 1. But no checkbox with Unlimited selected.

knalstaaf’s picture

Make sure that your field isn't defined as a "select list" in your Manage Fields tab. It should be "radio/checkboxes" or something.

There should only be one value in it (eg "1|Yes, send me the catalogue as well"), and the Number of values should be set to "unlimited".

jimmins’s picture

Thanks for the response. I had it setup just like you described (radio/checkboxes, 1|Yes, unlimited), but I couldn't get it to work. For now, I just changed it to a Select List with Yes and No options. I may experiment with it later. Thanks again.

knalstaaf’s picture

Are we talking about a regular content type or a webform?

jimmins’s picture

Just a regular content type.

taote’s picture

Does anyone know how to have an unchecked checkbox as default value, when the field is marked as mandatory?

WtheM’s picture

Good question. I am interested too.

rabihs’s picture

Any luck with that, I need it as well

quagmire’s picture

on a solution?

Tino’s picture

I'm wondering too: http://drupal.org/node/1143516#comment-6375992
Any progress here?

extexan’s picture

It's never too late to have a happy childhood. ;-)

selwynpolit’s picture

Add a new field:
Type: boolean
Widget: Single on/off checkbox
Label: "I agree to the terms and conditions"
Check required
Help text: Please check to agree with our Terms and Conditions
Check Use field label instead of the "On value" as label

ionmedia’s picture

works good

operations’s picture

It works.. Awesome!

leoklein’s picture

This is how I set the thing up. But the problem is Views doesn't like it and I get the error 'No valid values found on filter' [...]

dawid_nawrot’s picture

this answer should show up as the first search result in google as far as single checkbox required field in Drupal 7 content type