Skip to content

Commit

Permalink
Merge pull request #582 from azmeuk/issue-572
Browse files Browse the repository at this point in the history
Bugfix, tests and documentation for SelectField choices shortcut
  • Loading branch information
davidism committed Apr 22, 2020
2 parents 910e879 + 1cd8781 commit 55009f7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
.. currentmodule:: wtforms


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

Unreleased

- Fixed a bug when :class:`~wtforms.fields.core.SelectField` choices
is ``None``. :issue:`572, 585`


Version 2.3.0
-------------

Expand Down
10 changes: 6 additions & 4 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ refer to a single input from the form.

.. class:: SelectField(default field arguments, choices=[], coerce=unicode, option_widget=None, validate_choice=True)

Select fields keep a `choices` property which is a sequence of `(value,
label)` pairs. The value portion can be any type in theory, but as form
data is sent by the browser as strings, you will need to provide a function
which can coerce the string representation back to a comparable object.
Select fields take a ``choices`` parameter which is a list of
``(value, label)`` pairs. It can also be a list of only values, in
which case the value is used as the label. The value can be any
type, but because form data is sent to the browser as strings, you
will need to provide a ``coerce`` function that converts a string
back to the expected type.

**Select fields with static choice values**::

Expand Down
15 changes: 15 additions & 0 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ def test_dont_validate_choices(self):
self.assertEqual(form.a.data, "b")
self.assertEqual(len(form.a.errors), 0)

def test_choice_shortcut(self):
F = make_form(a=SelectField(choices=["foo", "bar"], validate_choice=False))
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_empty_choice(self):
F = make_form(a=SelectField(choices=[], validate_choice=False))
form = F(a="bar")
self.assertEqual(form.a(), """<select id="a" name="a"></select>""")

def test_none_choice(self):
F = make_form(a=SelectField(choices=None, validate_choice=False))
form = F(a="bar")
self.assertEqual(form.a(), """<select id="a" name="a"></select>""")


class SelectMultipleFieldTest(TestCase):
class F(Form):
Expand Down
4 changes: 3 additions & 1 deletion wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ def __init__(self, label=None, validators=None, coerce=text_type, choices=None,
self.validate_choice = validate_choice

def iter_choices(self):
if isinstance(self.choices[0], (list, tuple)):
if not self.choices:
choices = []
elif isinstance(self.choices[0], (list, tuple)):
choices = self.choices
else:
choices = zip(self.choices, self.choices)
Expand Down

0 comments on commit 55009f7

Please sign in to comment.