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.

Comments

roflcopterDorrie created an issue. See original summary.

roflcopterDorrie’s picture

Version: 7.38 » 7.x-dev
roflcopterDorrie’s picture

StatusFileSize
new634 bytes
roflcopterDorrie’s picture

Status: Active » Needs review
droplet’s picture

roflcopterDorrie’s picture

I 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.

roflcopterDorrie’s picture

Status: Closed (duplicate) » Active
JeroenD’s picture

I have the same problem. Applying the patch from #3 fixed it.
Thanks roflcopterDorrie!

idebr’s picture

Status: Active » Closed (duplicate)
Related issues: +#1239930: states.js appends span.form-required to every label of a dependent field

Hi 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.