With the patch, I can have all the checkboxes toggle on/off based on the checked state of another checkbox.I think it makes sense to support this for checkboxes. For radios, this probably doesn't make much sense.

However, what I really want is to check all the checkboxes when the other checkbox is checked but leave the checkboxes alone when that other checkbox is unchecked. It seems the #state system is unable to do this and we have to do custom javascript for this kind of functionality. Is this correct?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Status: Active » Needs review

Always set issue status to needs review when there is a patch.

mattyoung’s picture

Title: Form #state does not work on a "checkboxes" element » Form #states does not work on a "checkboxes" element
FileSize
809 bytes

Simplify a little because drupal_render() does this:

  // Add any JavaScript state information associated with the element.
  if (!empty($elements['#states'])) {
    drupal_process_states($elements);
  }

So we can put NULL in '#states' and that will not be processed.

I use !empty($elements['#states']) instead of isset($elements['#states']) like the '#ajax' key becuase drupal_render() uses !empty().

kalebheitzman’s picture

Applied the patch on clean install and the install still works fine. I'm not sure how to actually test the functionality of this patch.

mattyoung’s picture

You won't see anything different with this patch unless you use the '#states' on a "checkboxes" form element like this:

  $form['fruits'] = array(
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc(array(t('Apple'), t('Banana'), t('Peach'), t('Orange'))),
    '#title' => t('What do you want to eat?'),
    '#states' => array(
      'checked' => array(
        ':input[name="eat_all"]' => array('checked' => TRUE),
      ),
    ),
  );

  $form['eat_all'] = array(
    '#type' => 'checkbox',
    '#title' => t('Eat all'),
  );

Before the patch, clicking on the "Eat all" checkbox does nothing to the "fruits" checkboxes. With the patch, all the checkboxes in "fruits" follow the check state of "eat_all".

Berdir’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Moving to the 8.x queue and tagging for possible backport.

ryan.gibson’s picture

Alright, just going to post my results here - I'm not exactly sure what was trying to be accomplished. But I think I got it.

Pre patch, the select box did not affect the dependee checkboxes. No matter what I did, they remained. (first screenshot).

After applying the patch, the select box did in fact turn the dependee checkboxes on or off accordingly. (screenshots 2 and 3.)

That is all. :)

ryan.gibson’s picture

Sorry I didn't mention before - I tested the above in FF and Webkit browsers.

star-szr’s picture

Reroll for 8.x.

Status: Needs review » Needs work

The last submitted patch, checkboxes-states-1032062-8.patch, failed testing.

xjm’s picture

Status: Needs work » Needs review

Was a bot issue.

There is now an 8.x branch of the #states exercise module available:
http://drupal.org/project/1269964/git-instructions

dawehner’s picture

This issue seems to be a duplicate of #994360: #states cannot check/uncheck checkboxes elements. The other one is older, has more comments and even fixes radios.

Garrett Albright’s picture

Issue summary: View changes
Status: Needs review » Closed (duplicate)

I concur that this is a dupe. Closing.