Trouble with Form API and #states I have a form built and need a fieldset to collapse/expand based on the value of another checkbox field.

The checkbox code is:

$form[$serving_key] = array(
        '#type' =&gt; 'checkbox',
        '#title' =&gt; t($serve_size)
);


The fieldset is:

$form[$fieldset_key] = array(
        '#type' =&gt; 'fieldset',
        '#collapsible' =&gt; TRUE,
        '#collapsed' =&gt; TRUE,
        '#states' =&gt; array(
            'expanded' =&gt; array(
                ':input[name="' . $serving_key . '"]' =&gt; array('checked' =&gt; TRUE)
            )
        )
);


These are in a foreach loop, and $serving_key at any point is 'serving_large', 'serving_small', etc.  The corresponding $fieldset_key is 'fieldset_large', 'fieldset_small', etc.

This looks like it should work in the form_example_states_form example code.  What am I missing?
You forgot about unique keys in your form elements. Here is a working example:

function test_form($form, &amp;$form_state) {

  // your size options
  $options = array('small', 'medium', 'big');

  $form['checkboxes'] = array(
    '#type' =&gt; 'fieldset',
  );

  foreach ($options as $size) {

    $form['checkboxes'][$size . '_checkbox'] = array(
      '#type' =&gt; 'checkbox', 
      '#title' =&gt; t($size . ' size'),
    );

    $form[$size . '_field'] = array(
      '#type' =&gt; 'fieldset', 
      '#title' =&gt; t($size . ' size'), 
      '#collapsible' =&gt; TRUE,
      '#collapsed' =&gt; TRUE,
      '#states' =&gt; array(
        'expanded' =&gt; array(
          ':input[name="' . $size. '_checkbox"]' =&gt; array('checked' =&gt; TRUE),
        ),
      ),
    );

    $form[$size . '_field']['some_info'] = array(
      '#type' =&gt; 'textarea', 
      '#title' =&gt; t($size . ' size textarea'),
    );
  }
  return $form;
}


Result: