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

SelectMultipleField default values are ignored when a request object exists #604

Closed
azmeuk opened this issue May 16, 2020 · 1 comment
Closed
Labels
bug Unexpected behavior

Comments

@azmeuk
Copy link
Member

azmeuk commented May 16, 2020

When a request object and a data dict are passed to a form, the data dict values for SelectMultipleField are ignored.

>>> import wtforms, werkzeug
>>> choices=[("foo", "foo"), ("bar", "bar"), ("baz", "baz")]
>>> requestform = werkzeug.datastructures.ImmutableMultiDict([])

>>> class F(wtforms.Form):
...     multiple = wtforms.SelectMultipleField(choices=choices)

>>> F(multiple=["foo", "bar"]).multiple()
Markup('<select id="multiple" multiple name="multiple"><option selected value="foo">foo</option><option selected value="bar">bar</option><option value="baz">baz</option></select>')

>>> F(requestform, multiple=["foo", "bar"]).multiple()
Markup('<select id="multiple" multiple name="multiple"><option value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>')
>>> # Nothing is 'selected' here...

A simple workaround is F(requestform or None, multiple=["foo", "bar"]).multiple() but I think this should probably work without it.

The problem does not exist with SelectField:

>>> class G(wtforms.Form):
...     single = wtforms.SelectField(choices=choices)
>>> G(requestform, single="foo").single()
Markup('<select id="single" name="single"><option selected value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>')

Environment

  • Python version: 3.8
  • wtforms version: 2.3.1 and master
@azmeuk azmeuk added the bug Unexpected behavior label May 16, 2020
@davidism
Copy link
Member

I think this is a bug with SelectField, not SelectMultipleField. If form data was passed in, default data should be ignored. But with many web frameworks it's impossible to tell that a form wasn't submitted vs submitted but empty by only looking at the form data object. This is a specific case of #402.

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

No branches or pull requests

2 participants