Opened 6 years ago

Closed 6 years ago

Last modified 6 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 by Tim Kleinschmidt, 6 years ago

Type: UncategorizedBug

comment:2 by Tim Graham, 6 years ago

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

comment:3 by Tim Graham, 6 years ago

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 by Claude Paroz, 6 years ago

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

comment:5 by Claude Paroz, 6 years ago

Has patch: set

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

Resolution: fixed
Status: newclosed

In f3b69f97:

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

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.

comment:7 by Tim Graham <timograham@…>, 6 years ago

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 by Tim Graham <timograham@…>, 6 years ago

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