Hi,
I have found that after using hook_form_alter() to update a form with states information, the javascript is applying required labels on all labels under a radio button, rather than just the top label.
I have attached a screenshot of an example. When this field is made visible and required, the js is attached the required label to all labels under that field, resulting in an asterisks appearing on all the radio button labels.
In the states.js file I can see that the javascript is applying to every label.
$(e.target).closest('.form-item, .form-wrapper').find('label').append('<span class="form-required">*</span>');
My proposed fix is to only apply the required span to the first label eg
$(e.target).closest('.form-item, .form-wrapper').find('label').first().append('<span class="form-required">*</span>');
Is this an appropriate fix?
I have done some basic testing and it appears to work correctly for me.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | states_required_on_all_labels-2556909-3.patch | 634 bytes | roflcopterDorrie |
| radio.png | 4.21 KB | roflcopterDorrie |
Comments
Comment #2
roflcopterDorrie commentedComment #3
roflcopterDorrie commentedComment #4
roflcopterDorrie commentedComment #5
droplet commentedComment #6
roflcopterDorrie commentedI had previously already applied the patch from https://www.drupal.org/node/1592688#comment-10250629 and this issue is still occurring. That patch definitely removed the double asterisks from the labels, but this is a different problem.
The issue is not that it is being applied twice, it is that it is being applied to all labels under the field.
Since each radio button has its own label, there are asterisks appearing on all radio button labels, like in the screenshot I provided.
Comment #7
roflcopterDorrie commentedComment #8
JeroenD commentedI have the same problem. Applying the patch from #3 fixed it.
Thanks roflcopterDorrie!
Comment #9
idebr commentedHi roflcopterDorrie,
This issue was fixed in Drupal 8 in #1239930: states.js appends span.form-required to every label of a dependent field and that solution can be ported back to Drupal 7.
I'll close this issue as a duplicate, so we can focus our efforts in the related issue.