I have a field of type Text with 'Single on/off checkbox' widget. The list of allowed values is configured as follows:

0|off
1|on

It is not possible to save 'off' value to this field - the field always contains 'on' value. I also tried to use the field of type Integer but to no avail.

Comments

yched’s picture

Status: Active » Closed (works as designed)

That's actually the intended behavior for the on/off widget.
If checked, the 'on' value is saved, if unchecked, the field is considered empty and saves NULL.

paldr’s picture

I probably did not make myself clear - it is not possible to save any other value except 'on'. The checkbox always remains in 'checked' position.

yched’s picture

Title: Unable to save value using checkbox widget » unchecking on/off widget doesn't take
Status: Closed (works as designed) » Active

Oh, right, that wasn't clear. I can reproduce that.

Sigh. I thought we had fixed that already. Will need to investigate.

yched’s picture

Title: unchecking on/off widget doesn't take » unchecking optionwidgets doesn't take
Priority: Normal » Critical

Seems to be the case also for selects and radios/checkboxes.
Something's wrong in optionwidgets_validate() / optionwidgets_form2data().

This is a recurrent pain, so for now, I'm focusing on a comprehensive test suite for optionwidgets :-)

yched’s picture

I committed a suite of tests for optionwidgets.
Tests that fail :
- select : unselect all in multiple select doesn't take
- radios/checkboxes : uncheck all checkboxes doesn't take
- on/off checkbox : uncheck doesn't take
(same bug, as seen by the different widget types)

ppatriotis’s picture

I also encountered this problem when unchecking all of the checkboxes in an OptionWidget. I found that when nothing was selected in the form, the post omitted the field altogether, and I guess the Forms API filled in the values using the original default ones.

My solution was to change the first line in optionwidgets_form2data from

$items = $element['#value'];

to

$items = isset($element['#post'][$field['field_name']]) ? $element['#value'] : array();

I'm not sure if this will work for every situation but it works for my arrangement of OptionWidgets. I hope this helps.

yched’s picture

Status: Active » Fixed

In fact, it appears the correct FAPI #value to use is $element[$field_key]['#value'], rather than $element['#value'] which is wrong when $POST has no data for the field. Fixed now.

While the issue is now fixed, the optionwidgets simpletests still fail at the same spot :-p
Either a bug in DrupalWebTestCase::drupalPost(), or I'm not using it the right way...

Well, marking this issue fixed for now

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.