Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "for" attribute from RadioField associtated label element #775

Open
agascar opened this issue Feb 25, 2023 · 1 comment
Open

Remove "for" attribute from RadioField associtated label element #775

agascar opened this issue Feb 25, 2023 · 1 comment

Comments

@agascar
Copy link

agascar commented Feb 25, 2023

Actual Behavior

When using a RadioField, the "for" attribute of the associate label, points to the id of a list, whitch is incorrect since list are not form controls, this cause problems with html validators like https://validator.w3.org/

>>> from wtforms import Form, RadioField
>>> class RadioFieldTest(Form):
...     radio_field = RadioField("Options",choices=["Option 1","Option 2"])
...
>>> rt_form = RadioFieldTest()
>>> print(rt_form.radio_field.label())
<label for="radio_field">Options</label>
>>> print(rt_form.radio_field())
<ul id="radio_field"><li><input id="radio_field-0" name="radio_field" type="radio" value="Option 1"> <label for="radio_field-0">Option 1</label></li><li><input id="radio_field-1" name="radio_field" type="radio" value="Option 2"> <label for="radio_field-1">Option 2</label></li></ul>

Expected Behavior

# The expecter behavior is <label >Options</label>, without the "for" attribute

>>> from wtforms import Form, RadioField
>>> class RadioFieldTest(Form):
...     radio_field = RadioField("Options",choices=["Option 1","Option 2"])
...
>>> rt_form = RadioFieldTest()
>>> print(rt_form.radio_field.label())
<label >Options</label>
>>> print(rt_form.radio_field())
<ul id="radio_field"><li><input id="radio_field-0" name="radio_field" type="radio" value="Option 1"> <label for="radio_field-0">Option 1</label></li><li><input id="radio_field-1" name="radio_field" type="radio" value="Option 2"> <label for="radio_field-1">Option 2</label></li></ul>

Environment

  • Python version: 3.10.7
  • wtforms version: 3.0.1
@MegMM
Copy link

MegMM commented Feb 6, 2024

Could this cause an Incorrect use of <label for=FORM_ELEMENT> error?

sort_order = RadioField(u'Sort Order', choices=sort_order_values, 
                            default='descend', validators=[DataRequired()])
    <form-item class="radio-group">
      {{ form.sort_order.label(class="radio-label", id=False) }}
      {% for btn in form.sort_order -%}
      {{ btn(class="radio-btn") }}
      {{ btn.label(class="radio-value") }}
      {%- endfor %} 
    </form-item>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants