Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#29273 closed Bug (fixed)

MultipleChoiceField incorrectly selects the empty value for an empty form

Reported by: Tim Kleinschmidt Owned by: nobody
Component: Forms Version: 1.11
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

hi,

I upgraded Django from 1.8 to 1.11, in the last month and we have now a bug that the empty choice is always selected on initial page load and leads to confusion. If I deselect and select the value it work like intended.

It seems for me that there is no check if something was already selected or if it be a fresh form. Like it was in 1.8 in selected_choices

I think this report also apply to the ModelMultipleChoiceField

e.g.

Code highlighting:

choices = [(u'', '(no extra)'), (u'cheese', u'Cheese'), (u'Pepper', u'Pepper')]
extra_topics = forms.MultipleChoiceField(required=False, choices=choices)

Change History (8)

comment:1 Changed 2 years ago by Tim Kleinschmidt

Type: UncategorizedBug

comment:2 Changed 2 years ago by Tim Graham

Can you please be more explicit about the steps to reproduce the issue?

comment:3 Changed 2 years ago by Tim Graham

Severity: NormalRelease blocker
Summary: MultipleChoiceField auto selects on initial load the empty valueMultipleChoiceField incorrectly selects the empty value for an empty form
Triage Stage: UnreviewedAccepted

The difference can be seen here:

from django import forms
choices = [('', '(no extra)'), ('cheese', 'Cheese'), ('Pepper', 'Pepper')]
class Form(forms.Form):
    extra_topics = forms.MultipleChoiceField(required=False, choices=choices)
    
form = Form()
print(form)

The first option renders as <option value="" selected>(no extra)</option> in Django 1.11+ but without selected in older versions.

comment:4 Changed 2 years ago by Claude Paroz

Not surprisingly, this can be bisected to the template-based widget rendering commit [b52c73008a9d67e9ddbb].

comment:5 Changed 2 years ago by Claude Paroz

Has patch: set

comment:6 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In f3b69f97:

Fixed #29273 -- Prevented initial selection of empty choice in multiple choice widgets.

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

In 160829d3:

[2.0.x] Fixed #29273 -- Prevented initial selection of empty choice in multiple choice widgets.

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.

Backport of f3b69f9757ec03057441ebbd52b7cdbfed31fb32 from master.

comment:8 Changed 2 years ago by Tim Graham <timograham@…>

In b25433a:

[1.11.x] Fixed #29273 -- Prevented initial selection of empty choice in multiple choice widgets.

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.

Backport of f3b69f9757ec03057441ebbd52b7cdbfed31fb32 from master.

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