Skip to content

Commit

Permalink
Fix SelectField choice backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Oct 26, 2023
1 parent 4526b19 commit a7a87be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/wtforms/fields/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def __iter__(self):
_form=None,
_meta=self.meta,
)
for i, (value, label, checked, render_kw) in enumerate(self.iter_choices()):
for i, choice in enumerate(self.iter_choices()):
if len(choice) == 4:
value, label, checked, render_kw = choice
else:
value, label, checked = choice
render_kw = {}

opt = self._Option(
label=label, id="%s-%d" % (self.id, i), **opts, **render_kw
)
Expand Down Expand Up @@ -141,7 +147,7 @@ def pre_validate(self, form):
if self.choices is None:
raise TypeError(self.gettext("Choices cannot be None."))

for _, _, match, _ in self.iter_choices():
for _, _, match, *_ in self.iter_choices():
if match:
break
else:
Expand Down
16 changes: 13 additions & 3 deletions src/wtforms/widgets/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Select:
The field must provide an `iter_choices()` method which the widget will
call on rendering; this method must yield tuples of
`(value, label, selected, render_kw)`.
`(value, label, selected)` or `(value, label, selected, render_kw)`.
It also must provide a `has_groups()` method which tells whether choices
are divided into groups, and if they do, the field must have an
`iter_groups()` method that yields tuples of `(label, choices)`, where
Expand All @@ -358,11 +358,21 @@ def __call__(self, field, **kwargs):
if field.has_groups():
for group, choices in field.iter_groups():
html.append("<optgroup %s>" % html_params(label=group))
for val, label, selected, render_kw in choices:
for choice in choices:
if len(choice) == 4:
val, label, selected, render_kw = choice
else:
val, label, selected = choice
render_kw = {}
html.append(self.render_option(val, label, selected, **render_kw))
html.append("</optgroup>")
else:
for val, label, selected, render_kw in field.iter_choices():
for choice in field.iter_choices():
if len(choice) == 4:
val, label, selected, render_kw = choice
else:
val, label, selected = choice
render_kw = {}
html.append(self.render_option(val, label, selected, **render_kw))
html.append("</select>")
return Markup("".join(html))
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def basic_widget_dummy_field(dummy_field_class):

@pytest.fixture
def select_dummy_field(dummy_field_class):
return dummy_field_class([("foo", "lfoo", True, {}), ("bar", "lbar", False, {})])
return dummy_field_class([("foo", "lfoo", True), ("bar", "lbar", False)])


@pytest.fixture
Expand Down

0 comments on commit a7a87be

Please sign in to comment.