I'm not sure if this affects other widgets but it certainly affects the single on/off checkbox.

I have a set of radios and depending on which is selected I want to check or uncheck the single on/off checkbox field.

I have tried "Checked", "Unchecked", "Emptied" but none of them do anything.

It is the only condition on this checkbox field.

I have tried with most of the different "Values input mode" options but it makes no difference.

Comments

Martin.’s picture

Confirming this issue. This is also present in 7.x-3.x-dev

tky’s picture

Confirmed this issue.

"Unchecked" and "Emptied" option does not work on my boolean field as a checkbox field.

apmsooner’s picture

Have you tried manually entering the on value "1" in most cases rather then using the widget defining value?

rooby’s picture

I have tried pretty much all variations I could think of and nothing worked.

tky’s picture

Confirmed robby's situation.

vtkachenko’s picture

Faced same issue. I found that state action attached to field wrapper, but not to form element.

In debug console you can see that js action runs on field wrapper - it really sets values for attribute checked.

I couldn't find suitable alter in module. Which allow to change target element.

For my project I use quick tricky script:

(function ($) {

  $(document).bind('state:checked', function (e) {

    // Fix issue with Conditional fields. It applies actions to element wrapper.
    // We re-implement it to form element instead.
    if (e.trigger && $(e.target).hasClass('field-widget-options-onoff')) {
      $(e.target).removeAttr('checked').find('input.form-checkbox').attr('checked', e.value);
    }
  });

})(jQuery);
Chipie’s picture

#6 works for me. Thank you for sharing

ray17n’s picture

#6 also works for me. Thanks a lot.

eevensen’s picture

#6 also works for me.
Thank you, Vitalii T. (vtkachenko).

Add a new javascript file with the code above to your theme folder:
/sites/default/all/themes/[your_theme]/js/[filename].js

Add the following to your theme's info file.
"scripts[] = js/[filename].js"

See guide: https://www.drupal.org/node/756722

glass.dimly’s picture

Didn't work with jQuery 1.10. Small change, however, fixes it: .prop('checked', e.value);

Also, I'm attaching using js behaviors:

(function($, Drupal, window, document) {
  'use strict';

  Drupal.behaviors.my_module = {

    attach: function(context, settings) {
      $(document).bind('state:checked', function (e) {
        // Fix issue with Conditional fields. It applies actions to element wrapper.
        // We re-implement it to form element instead.
        if (e.trigger && $(e.target).hasClass('field-widget-options-onoff')) {
          $(e.target).removeAttr('checked').find('input.form-checkbox').prop('checked', e.value);
        }
      });
    }
  };

})(jQuery, Drupal, this, this.document);

...and I'm attaching to the form like so:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function my_module_states_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (!isset($form['#attached']['js'])) {
    $form['#attached']['js'] = array();
  }
  $form['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'my_module') . '/js/my_module.js',
    'type' => 'file',
  );
}
siavash’s picture

#10 worked for me, thanks!

dqd’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Priority: Major » Normal
Status: Active » Closed (outdated)

Thanks for the report and all the efforts in here. But due to inactivity in this issue for some years and because of the upcoming EOL of Drupal 7, I will close this issue on the way cleaning up the issue queue.

Feel free to re-open as "Needs review" if you found a solution or have a patch to be reported to help others and I will see if we can commit it before Drupal 7 EOL.