2

I'm trying to use this to achieve the functionality of having a checkbox that, when selected, causes a range of checkboxes to all be selected. I made the selectall box a separate form field, and it looks like this, and works not a lick:

$form['report_group']['selectall'] = array(
  '#type' => 'checkbox',
  '#default_value' => 0,
);

$form['report_group']['boxes'] = array(
  '#type' => 'checkboxes',
  '#options' => $boxes,
  '#states' => array(
    'checked' => array(
      ':input[name="report_group[selectall]"]' => array('checked' => TRUE),   
    ),
  ),
2
  • Is the missing ); just a typo in the question?
    – Clive
    Apr 23, 2014 at 14:34
  • Yes, it was omitted accidentally.
    – user27954
    Apr 23, 2014 at 19:41

1 Answer 1

0

I don't think the "checked" property works with the "checkboxes" form control (or if I does I know how to use it!)

As a work around use the checkbox type instead:

$form['report_group']['selectall'] = array(
  '#title' => 'Check All',
  '#type' => 'checkbox',
  '#default_value' => 0,
);

$boxes = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');

foreach($boxes as $key => $val) {

  $form['report_group']['box_'.$key] = array(
    '#type' => 'checkbox',
    '#title' => $val,
    '#return_value' => $key,
    '#states' => array(
      'checked' => array(
        ':input[name="selectall"]' => array('checked' => TRUE),
      ),
    ),
  );
}

Your selector was also wrong. This is a jQuery selector so I find it easiest to use firebug to figure out what it should be and test if it's working.

2
  • I'm not sure that the selector was wrong. The rest of the form isn't shown, and these fields belong to a fieldset. The docs say that when that is the case, the selector must contain the fieldset name, as I have it.
    – user27954
    Apr 23, 2014 at 19:43
  • Got ya - the fieldset will change the elements name.
    – Felix Eve
    Apr 24, 2014 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.