#27250: Confusing <label> assignment with CheckboxSelectMultiple
----------------------------+------------------------------------------
     Reporter:  lamda       |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Forms       |    Version:  1.10
     Severity:  Normal      |   Keywords:  CheckboxSelectMultiple label
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+------------------------------------------
 Using CheckboxSelectMultiple leads to a (in my opinion) confusing
 assignment of the <label> in the generated HTML. The general label for the
 choices refers to the first element in the list. Please see this example:

 forms.py:
 {{{
 class TestForm(forms.Form):
     days = forms.MultipleChoiceField(
         widget=forms.CheckboxSelectMultiple,
         choices=[('1', 'DayOne'), ('2', 'DayTwo')],
     )
 }}}

 views.py:
 {{{
 def form(request):
     return render(request, 'form.html', {'form': forms.TestForm()})
 }}}
 form.html:
 {{{
 <form method="post">
   <ul>
     {{ form.as_ul }}
   </ul>
   <button type="submit">Submit</button>
 </form>
 }}}

 Resulting HTML (prettified):
 {{{
 <form method="post">
   <ul>
     <li>
       <label for="id_days_0">Days:</label>
       <ul id="id_days">
         <li>
           <label for="id_days_0"><input id="id_days_0" name="days"
 type="checkbox" value="1" /> DayOne</label>
         </li>
         <li>
           <label for="id_days_1"><input id="id_days_1" name="days"
 type="checkbox" value="2" /> DayTwo</label>
         </li>
       </ul>
     </li>
   </ul>
   <button type="submit">Submit</button>
 </form>
 }}}

 This means that clicking on "Days" selects the first day. I would suggest
 to remove the "<label> element and only keep the label text itself.

 Compare this to the HTML when not having the CheckboxSelectMultiple
 widget, where clicking on "Days" simply selects the <select> element:
 {{{
 <form method="post">
   <ul>
     <li>
       <label for="id_days">Days:</label>
       <select multiple="multiple" id="id_days" name="days" required>
         <option value="1">One</option>
         <option value="2">Two</option>
       </select>
     </li>
   </ul>
   <button type="submit">Submit</button>
 </form>
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27250>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.1e640c1ec105e6b1a1357ec94d71af46%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to