Skip to content

Commit

Permalink
Merge pull request #809 from azmeuk/issue-735-selectfield-no-choice-n…
Browse files Browse the repository at this point in the history
…o-validation

Select(Multiple)Field choice can be None if validate_choice is False
  • Loading branch information
azmeuk authored Oct 15, 2023
2 parents f6b2ecd + 44718ef commit f5574d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Version 3.x.x
Unreleased

- Display :class:`~wtforms.Flags` values in their repr. :pr:`808`
- :class:`~fields.SelectField` and :class:`~fields.SelectMultipleField`
``choices`` can be `None` if `validate_choice` is `False` :pr:`809`

Version 3.1.0
-------------
Expand Down
12 changes: 6 additions & 6 deletions src/wtforms/fields/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ def process_formdata(self, valuelist):
raise ValueError(self.gettext("Invalid Choice: could not coerce.")) from exc

def pre_validate(self, form):
if self.choices is None:
raise TypeError(self.gettext("Choices cannot be None."))

if not self.validate_choice:
return

if self.choices is None:
raise TypeError(self.gettext("Choices cannot be None."))

for _, _, match, _ in self.iter_choices():
if match:
break
Expand Down Expand Up @@ -189,12 +189,12 @@ def process_formdata(self, valuelist):
) from exc

def pre_validate(self, form):
if self.choices is None:
raise TypeError(self.gettext("Choices cannot be None."))

if not self.validate_choice or not self.data:
return

if self.choices is None:
raise TypeError(self.gettext("Choices cannot be None."))

acceptable = {c[0] for c in self.iter_choices()}
if any(d not in acceptable for d in self.data):
unacceptable = [str(d) for d in set(self.data) - acceptable]
Expand Down
6 changes: 6 additions & 0 deletions tests/fields/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def test_dont_validate_choices():
assert len(form.a.errors) == 0


def test_choices_can_be_none_when_choice_validation_is_disabled():
F = make_form(a=SelectField(validate_choice=False))
form = F(DummyPostData(a="b"))
assert form.validate()


def test_choice_shortcut():
F = make_form(a=SelectField(choices=["foo", "bar"], validate_choice=False))
form = F(a="bar")
Expand Down
6 changes: 6 additions & 0 deletions tests/fields/test_selectmultiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def test_dont_validate_choices():
assert len(form.a.errors) == 0


def test_choices_can_be_none_when_choice_validation_is_disabled():
F = make_form(a=SelectMultipleField(validate_choice=False))
form = F(DummyPostData(a="b"))
assert form.validate()


def test_requried_flag():
F = make_form(
c=SelectMultipleField(
Expand Down

0 comments on commit f5574d7

Please sign in to comment.