Opened 3 weeks ago

Last modified 25 hours ago

#36949 assigned Bug

Missing label on each <select> in a fieldset

Reported by: Jacob Walls Owned by: bhuvnesh-nama
Component: Forms Version: 6.0
Severity: Normal Keywords: accessibility, fieldset
Cc: Antoliny, Thibaud Colas, David Smith Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

As Thibaud mentioned here when use_fieldset was added to forms, we're lacking a label on each <select> element in a <fieldset>, even though a <legend> is present for the fieldset.

Now that the admin uses Widget.use_fieldset since #35892, it's easy to reproduce by just checking a ForeignKey field in the admin:

Then, in Chrome Dev Tools, the lighthouse tab reports:

Select elements do not have associated label elements.

Pure forms reproducer, in a shell:

from django.db import models
from django import forms

class Person(models.Model):
    best_friend = models.ForeignKey(
        "auth.User",
        models.CASCADE,
    )
    class Meta:
        app_label = "myapp.Person"

class MyWidget(forms.Select):
    use_fieldset = True

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ["best_friend"]
        widgets = {"best_friend": MyWidget}

print(PersonForm().as_p())
<p>
    <label for="id_best_friend">Best friend:</label>
    <select name="best_friend" required id="id_best_friend">
  <option value="" selected>---------</option>

  <option value="2">anonymous</option>

  <option value="1">admin</option>

</select>
    
    
      
    
  </p>

Tentatively assigning to Eli as a Djangonaut Space navigator to evaluate if a good fit for anyone.

Change History (6)

comment:1 by Jacob Walls, 3 weeks ago

There's some relevant advice in a related ticket: ticket:36509#comment:1

comment:2 by Natalia Bidart, 2 weeks ago

Cc: Eliana Rosselli added
Triage Stage: UnreviewedAccepted

Thank you!

comment:3 by Bhuvnesh Nama, 39 hours ago

Submitted PR for review.

PR: https://github.com/django/django/pull/20933

comment:4 by Jacob Walls, 38 hours ago

Cc: Eliana Rosselli removed
Has patch: set
Owner: changed from Eliana Rosselli to bhuvnesh-nama

comment:5 by Jacob Walls, 37 hours ago

Cc: David Smith added
Needs tests: set
Patch needs improvement: set

comment:6 by Bhuvnesh Nama, 25 hours ago

I've pushed updates to the pull request to address the review feedback, including adding tests and updating the implementation.

PR: https://github.com/django/django/pull/20933

Note: See TracTickets for help on using tickets.
Back to Top