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.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | webform-fieldset_ids-1613458-14.patch | 677 bytes | rooby |
| #13 | 1613458-states-fieldset-13.patch | 817 bytes | bbinkovitz |
| #12 | 767212-states-fieldset-15.patch | 693 bytes | bbinkovitz |
| #7 | 1613458-7-by-DeFr-Make-webform-fieldsets-compati.patch | 1.66 KB | DeFr |
Comments
Comment #1
micnap commentedBump. 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
Comment #2
nod_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?
Comment #3
Bastlynn commentedOne 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.
Comment #4
holdmann commentedFor 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:
Luck!
Comment #5
quicksketchIt 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.
Comment #6
DeFr commented@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 ?)
Comment #7
DeFr commentedAttaching a patch with the fix mentioned above.
Comment #8
quicksketchAh! 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.
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.
Comment #9
quicksketchdrupal_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!
Comment #11
quicksketchI 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.
Comment #12
bbinkovitz commentedDid 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.
Comment #13
bbinkovitz commentedOops ignore previous patch I meant to post this one for the version 4 dev branch.
Comment #14
rooby commentedConditional 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.
Comment #15
liam morlandThanks for the patch. This needs a test.
Comment #17
m@ster commentedDo you have plan commit this to the next release?
Still doesn't working even for 7.x-4.23
tnx.
Comment #18
liam morlandThis needs a test.
Comment #19
liam morlandDrupal 7 is no longer supported. If this applies to a supported version, please re-open.