Skip to content

Commit

Permalink
Merge pull request #639 from azmeuk/pr-598-backport
Browse files Browse the repository at this point in the history
Backported #598
  • Loading branch information
azmeuk authored Jul 29, 2020
2 parents bd03955 + a5fa621 commit 4333e38
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.. currentmodule:: wtforms


Unreleased
----------

- Fixed a bug with :class:`~fields.SelectField` choices shortcut at
form submission. :pr:`598, 639`


Version 2.3.1
-------------

Expand Down
7 changes: 7 additions & 0 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ def test_choice_shortcut(self):
form = F(a="bar")
self.assertEqual(form.a(), """<select id="a" name="a"><option value="foo">foo</option><option selected value="bar">bar</option></select>""")

def test_choice_shortcut_post(self):
F = make_form(a=SelectField(choices=["foo", "bar"]))
form = F(DummyPostData(a=["foo"]))
assert form.validate()
assert form.a.data == "foo"
assert len(form.a.errors) == 0

def test_empty_choice(self):
F = make_form(a=SelectField(choices=[], validate_choice=False))
form = F(a="bar")
Expand Down
4 changes: 2 additions & 2 deletions wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ def process_formdata(self, valuelist):

def pre_validate(self, form):
if self.validate_choice:
for v, _ in self.choices:
if self.data == v:
for _, _, match in self.iter_choices():
if match:
break
else:
raise ValueError(self.gettext("Not a valid choice"))
Expand Down

0 comments on commit 4333e38

Please sign in to comment.