When I'm trying to use FAPI #states for submit button I'm getting an error:
Error: Syntax error, unrecognized expression: #
in browser's console.
So, I decided to debug Webform module and found that it removes id attribute for the button in pre-render function webform_pre_render_remove_id(), which is added in the webform_client_form(). But drupal_process_states() uses id attribute and it can not be empty. So, in the result we have an error, described above.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | webform-ids_for_states-2279723-4.patch | 1.32 KB | danchadwick |
Comments
Comment #1
danchadwick commented@quicksketch - Why are the ids intentionally removed? Do we still need to do this?
@aprogs - if you are using hook_form_alter to add your #states, why not remove the pre-render that serves to remove the ids?
Comment #2
aprogs commentedHi @DanChadwick,
I guess that there are weighty reasons for removing id attribute. So, I decided to ask about it here.
Thanks
Comment #3
quicksketchThe IDs are removed as part of an overall effort to reduce the use of IDs and increase the use of classes. Buttons in particular were a problem because if you had multiple forms on the page, the IDs of the submit buttons would keep changing out from under you, making any CSS that targeted them difficult to predict.
Restoring the IDs shouldn't cause any problems other than those we had before. It hadn't occurred to me that someone would want to use #states to hide/show the buttons themselves.
I'd suggest one of two approaches to fix this problem for your use-case:
1) In your form_alter(), remove the "webform_pre_render_remove_id" callback from the button #pre_render array.
2) Or perhaps just don't use #states and add your own JS.
We could also potentially put a fix in
webform_pre_render_remove_idthat checks if #states is set, and if so, leaves the IDs in place.Comment #4
danchadwick commentedI liked quicksketch's third option: ids are only removed if there are no #states defined. Tested and applied to 7.x-4.x and 8.x.
Comment #6
aprogs commentedThanks for paying attention to this issue.
Comment #8
danchadwick commented