Using a hook_form_alter as follows:

  $form["submitted"]["a_fieldset"]["#states"] = array (
    "visible" => array (
      ':input[name="submitted[insurance_enrollment_waiver][insurance_selection]"]' => array("value" => "enroll"),
    ),
  );

on forms with the array structure of $form["submitted"]["a_fieldset"]["#states"] has no effect.

A working example of hiding a fieldset with #states can be found at https://drupal.org/node/767268 under (see the link to https://drupal.org/files/issues/states_bug_demo_5.tgz ) Using this same method with forms that are generated in the format that the webform module generates does not work.

Forms built with the standard format, as follows, work correctly with #states:

  $form['a_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('This is a fieldset that should be hidable'),
    '#states' => array(
      'invisible' => array(
        ':input[name=hide_fieldset]' => array('checked' => TRUE),
      ),
    ),
  );

To reproduce this, create a webform form, make a hook_form_alter and append a #states that specifies a "visible" attribute to a fieldset and another textfield with a condition that is easy to toggle. You will note that the textfield will hide but the fieldset will not.

Comments

micnap’s picture

Bump. I'm having the same issue except I'm not using a fieldset. Trying to alter the #state in a webform which puts form elements in a 'submitted' array. My field gets hidden properly but does not show when the correct option is chosen.

Mickey

nod_’s picture

Version: 7.14 » 7.x-dev
Issue tags: +JavaScript

I'd say it's a webform issue. The element needs to be processed for the JS settings that are going on in #states to be added properly. Please check with them if the "submitted" form is processed.

When you add JS or CSS with #attached in the submitted form element, does it work?

Bastlynn’s picture

One of my coworkers found a possible way around this: Instead of using the ID of the form to select on (which webform overrides), use attributes to set a class, and use jquery in your selector based on the class instead.

holdmann’s picture

For using #states around inputs with square brackets in the name attribute, you have to escape meta-characters with 2 back-slashes.

In final, you should get something like this:

  $form['submitted']['e_mail']['#states'] = array(
    'required' => array(
      ':input[name=submitted\\[mailer\\]\\[1\\]]' => array('checked' => TRUE),
    ),
  );

Luck!

quicksketch’s picture

It looks like there was at one point a specific bug around fieldsets and #states: #767212: #states can't hide/show fieldsets and there's an additional issue still open at #1148950: Applying #states to a vertical tab does not update the vertical tabs menu. Webform doesn't prevent states from working in any way, so I don't think this is a Webform-specific issue. The example may be from Webform, but it's likely a global problem; though I'm not sure what the state of fieldsets + #states is.

DeFr’s picture

Project: Drupal core » Webform
Version: 7.x-dev » 7.x-4.x-dev
Component: forms system » Code

@quicksketch: I'm sorry to disagree, but I think the problem is Webform specific ; to be more precise, webform_fieldset_prerender ( http://drupalcode.org/project/webform.git/blob/refs/heads/7.x-3.x:/compo... ) sets the fieldset id using ['#attributes']['id'] instead of ['#id'] ; that id can't be picked up by drupal_process_states, and the rules are added on the wrong element.

(Presumably, this should also go through drupal_html_id to ensure that there's no duplicate id on the page ?)

DeFr’s picture

Status: Active » Needs review
StatusFileSize
new1.66 KB

Attaching a patch with the fix mentioned above.

quicksketch’s picture

Status: Needs review » Reviewed & tested by the community

@quicksketch: I'm sorry to disagree, but I think the problem is Webform specific ; to be more precise, webform_fieldset_prerender sets the fieldset id using ['#attributes']['id'] instead of ['#id']

Ah! Well I'm happy to be proven wrong. Thanks this is a good looking patch. Webform has to set its own ID in order to make it work correctly with its conditionals system. It hadn't occurred to me that using ['#attributes']['id'] instead of ['#id'] would have caused a problem.

(Presumably, this should also go through drupal_html_id to ensure that there's no duplicate id on the page ?)

Yeah, though actually I think we avoid drupal_html_id() because people (no idea how this happens) sometimes end up rendering the entire Webform twice even though it's only printed out once. Since Webform's own conditional logic is dependent upon the IDs not conflicting, we've been avoiding use of drupal_html_id(). Although that's a problem for those users. I think the patch looks good as-is and we should get it committed to all branches.

quicksketch’s picture

Title: Form #states do not affect fieldsets from within $form["submitted"]... » Form #states do not affect fieldsets from within $form["submitted"]
Status: Reviewed & tested by the community » Fixed

drupal_html_id() doesn't exist in D6 and neither do #states, so I decided to only commit this patch to the D7 versions of the module to avoid unexpected problems and porting to D6. Thanks @DeFr for your sleuthing on this one!

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

quicksketch’s picture

Issue summary: View changes

Since Webform's own conditional logic is dependent upon the IDs not conflicting, we've been avoiding use of drupal_html_id(). Although that's a problem for those users. I think the patch looks good as-is and we should get it committed to all branches.

I was mistaken. Using drupal_html_id() removes double hyphens in IDs (apparently), causing breakage. We're fixing this in #2243483: Change in Fieldset id generation.

bbinkovitz’s picture

StatusFileSize
new693 bytes

Did part of this get reverted? The latest dev seems to be setting an id of NULL for fieldsets and I'm having the same problem originally reported here. Attaching a rerolled patch.

bbinkovitz’s picture

StatusFileSize
new817 bytes

Oops ignore previous patch I meant to post this one for the version 4 dev branch.

rooby’s picture

Status: Closed (fixed) » Reviewed & tested by the community
StatusFileSize
new677 bytes

Conditional fieldset was not working for me until I applied this patch.
The patch in #13 fixes it for me.

Here is a reroll of #13 that doesn't put 2 lines of code on 1 line.

liam morland’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thanks for the patch. This needs a test.

  • quicksketch committed e0ea730 on 8.x-5.x
    Issue #1613458 by DeFr: Form #states do not affect fieldsets from within...
m@ster’s picture

Issue tags: -JavaScript +JavaScript

Do you have plan commit this to the next release?
Still doesn't working even for 7.x-4.23
tnx.

liam morland’s picture

This needs a test.

liam morland’s picture

Status: Needs work » Closed (outdated)

Drupal 7 is no longer supported. If this applies to a supported version, please re-open.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.