i am not sure i know the correct way to add a single checkbox item but this is the only way i could get it to work.

my field is defined as:

- integer
- checkbox
- multiple values
- allowed values:   "1|some text"

when i do a view to filter on this item i get:

    * warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\websites\eclipse\includes\database.mysql.inc on line 345.
    * warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\websites\eclipse\includes\database.mysql.inc on line 345.

backtracking through call stack a little i see a few odd things: (in reverse order)

- database.mysql.inc->db_escape_string is expecting a string and getting an empty array (hence the error above)

- views_query.inc->_views_replace_args is getting $args array as:
0: "node_data_field_feature_item"
1: "field_feature_item_value"
2: "OR"
3: empty array

the empty array is what is getting shifted off and passed up to cause the error.

other odditites:

- i have 3 nodes defined; 2 without the box checked; 1 with
- only the one with adds an entry (with a value of "1")
- i would have thought unchecked would save a value of "0" - perhaps this is the "default_value" post i just read??

- also not quite sure that the OR is correct???

also, and i guess would be expected.. but the view returns NO results - even though there should be 1 match.

for now i think i wll try using a yes/no radio button to get around this.. but my client really wants a checkbox.. which makes sense.

if the $args array above had:
2: "="
3: "1"

i think things would work correctly

Peter Lindstrom
LiquidCMS - Content Management Solution Experts

Comments

liquidcms’s picture

sorry , a couple other bits..

i am using latest cck 4.7 dev build - cck 4.7.x-1.x-dev from Jan 20th (today) and i did run update

i also tried this with version from a few days a go with same result.

yched’s picture

This rrreally looks like the errors thrown by views module when you forgot to actually select an item in your view field definition ('option' column or something)

karens’s picture

Well I can see now what the issue is but am not sure yet of how to address it. The way the optionwidgets module is configured, you create an array of allowable values. In this case, the only allowable value is '1'; When the user submits the form, the program checks if the value they have submitted is in the allowable values, and saves it only if it is. In this case, if someone leaves the box unchecked, they return a value of '0' which is not in the allowable values, so it is not saved.

So one option is to always save a zero value. But that won't work if the field is not required the zero value may be the wrong value (it should be NULL). And if the zero value is not defined in the allowable values list, is it really safe or correct to store that value? Who knows how many problems that might create.

Another option is to add the zero value to the allowed values list. But that will display the zero option as another checkbox, which is not what you want to do.

I think this is only an issue when you try to do this specific thing -- create a single 'on/off' type checkbox. So maybe it just requires a different widget type. I need to do some thinking on this, but ideas are welcome.

karens’s picture

http://drupal.org/node/112313 is a related but not a duplicate issue.

liquidcms’s picture

hi karen,

yup, sounds like you get what i am was describing.. well as long as I understand that i wasnt just configuring the checkbox incorrectly; i am ok for now... going ahead with the checked/unchecked radio button work around for now.

but if client comes back and really wants the single checkbox i will do the code to fix.

cheers,
peter...

karens’s picture

Status: Active » Fixed

It looks like the $form array to handle a single checkbox is so different from the normal checkboxes $form array that there really is no good way to do this except with its own widget, so I got something working as another widget option -- 'Single on/off checkbox' -- and committed it. This will need more testing, but I believe it is working right. Reopen if it doesn't work right.

Anonymous’s picture

Status: Fixed » Closed (fixed)
liquidcms’s picture

Category: bug » support
Status: Closed (fixed) » Postponed (maintainer needs more info)

still not sure quite how this is supposed to be used:

help text suggests:

For a single on/off checkbox, define the 'off' value first, then the 'on' value in the Allowed values section.

so i have this in Allowed Values:

0|no response
1|responded

so i now see a single checkbox with "responded" next to it.. and when i save "checked" i get a 1 in db; BUT, when i save "unchecked" i get NULL, shouldnt i get a 0?

i can likely work with it as null/1 but better if it was 0/1

liquidcms’s picture

still not sure quite how this is supposed to be used:

help text suggests:

For a single on/off checkbox, define the 'off' value first, then the 'on' value in the Allowed values section.

so i have this in Allowed Values:

0|no response
1|responded

so i now see a single checkbox with "responded" next to it.. and when i save "checked" i get a 1 in db; BUT, when i save "unchecked" i get NULL, shouldnt i get a 0?

i can likely work with it as null/1 but better if it was 0/1

liquidcms’s picture

Category: support » bug

sorry about the double post.. drupal.org getting hung up.

so i did the views test and as i suspected.. i did have to do the "is none of - responded" as opposed to the "one of - not responded" - snice it really would like there to be a 0 there as opposed to a null

so still a bug in single checkbox field type

plj’s picture

While I've no idea of 4.7, it is good to note that you can indeed do this in 5.x as follows:

  1. Create a new field as single on/off integer checkbox
  2. Give the default value for the field in PHP as follows: return array(0 => array('value' => 0));
  3. Set the field status to “required”. This is the key. Although it won't set 'not null default 0' constraint for the database column (which would be preferable), it should indeed give the same practical results as long as nodes are saved using Drupal's APIs
  4. Set the other fields as instructed: Minimum is 0, maximum is 1, and in “allowed values” field the first line is 0|off-label_of_your_choice and the second is 1|on-label_of_your_choice

That's it. Now zeros will be inserted into the database field instead of nulls, if the box is left empty.

(Actually I'm not sure how necessary the second step is; I had already done that, when I realised that the third one at least is essential.)

dopry’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)