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

SelectField will throw ValueError when using list of strings #598

Merged
merged 3 commits into from
Jun 14, 2020
Merged

SelectField will throw ValueError when using list of strings #598

merged 3 commits into from
Jun 14, 2020

Conversation

tresni
Copy link
Contributor

@tresni tresni commented Apr 26, 2020

This may assist with #464 (at least the behavior described by @ftm in that ticket.)

In the validation phase, we use iter_choices now instead of trying to iterate on choices directly. This will mean that regardless of list of tuples or the shortcut list of strings, we always have 3 values when we iterate the list. We use the match result from iter_choices instead of doing the comparison again to save the operation.

If you are using the shortcut list of strings and getting 'too many values to unpack (expected 2)', this is the fix.

I'm doing the following to work around this right now:

class TimeZoneForm(FlaskForm):
    timezone = SelectField('Timezone', choices=list(zip(listTimezones(), listTimezones())))

list(zip( is required to prevent choices from being an iterator. listTimezones() is a singleton function that returns a cached list of strings. Without the list(zip( or this patch, this will will always fail to validate.

This give a consistent output of 3 values where using just choices leads to ValueError when choices is a list of strings. By using match from iter_choices we also keep from doing the check twice so there should be no costs here except for the zip() when using lists of strings.
@azmeuk azmeuk merged commit 5db7dfb into wtforms:master Jun 14, 2020
@davidism davidism added this to the 3.0.0 milestone Jun 16, 2020
azmeuk added a commit to azmeuk/wtforms that referenced this pull request Jul 29, 2020
@azmeuk azmeuk mentioned this pull request Jul 29, 2020
azmeuk added a commit that referenced this pull request Jul 29, 2020
rriski added a commit to rriski/ihatemoney that referenced this pull request Mar 28, 2024
Error introduced in spiral-project#1290. Fixes spiral-project#1293. WTForms needs to be bumped to >=2.3.2
as it includes a fix to `SelectField` which is required for this change to work.

See:
  - https://wtforms.readthedocs.io/en/3.1.x/changes/#version-2-3-2
  - wtforms/wtforms#598
zorun pushed a commit to spiral-project/ihatemoney that referenced this pull request Mar 28, 2024
Error introduced in #1290. Fixes #1293. WTForms needs to be bumped to >=2.3.2
as it includes a fix to `SelectField` which is required for this change to work.

See:
  - https://wtforms.readthedocs.io/en/3.1.x/changes/#version-2-3-2
  - wtforms/wtforms#598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants