Not a Valid Choice for Dynamic Select Field WTFORMS
Asked Answered
J

4

55

I currently am creating a dynamic select field using WTFORMS, however it never submits and fails the validation with the following error.

Not a valid choice

My Field is created like this:

area = SelectField()

and in the view, i am grabbing the options from the db like so:

form = MytestForm()
form.area.choices = [(a.id, a.name) for a in Area.objects.all()]

It works however if i create static options.

Jung answered 20/12, 2012 at 1:56 Comment(1)
There a known issue related to this question see e.g. github.com/wtforms/wtforms/issues/434 but there are more. It's confusingTimid
D
117

My guess is that Area.id is a int - when data comes back from the client it is treated as a string by WTForms unless a callable is passed to the coerce keyword argument of the wtforms.fields.SelectField constructor:

area = SelectField(coerce=int)

Alternately, if you are using SQLAlchemy you could use wtforms.ext.sqlalchemy.fields.QuerySelectField (wtforms_sqlalchemy if you are using WTForms 3+):

area = QuerySelectField(query_factory=Area.objects.all,
                            get_pk=lambda a: a.id,
                            get_label=lambda a: a.name)
Ditzel answered 20/12, 2012 at 3:37 Comment(8)
Hi Sean, thanks for the response. However i am using mongodb, so the id is actually a string.Jung
Thanks it was the coerce issue, WTFORMS was defaulting to converting to unicode, however the actual object is a MongoDB Object ID type. so i changed the coerce to create an ObjectID instance, and it validates now.Jung
coerce=int fixed it for me. Specifically, my problem was that my choices values happened to be integers 0 and 1. My guess is that wtforms was interpreting this as False and True. However, my model wants an int. class Abc(Base): foo = Column(Integer) class AbcView(ModelView): form_overrides = { 'foo': SelectField, } form_args = { 'foo': { 'choices': [ (0, 'unknown'), (1, 'defcon 1') ], 'coerce': int } }Calumet
Dear @SeanVieira thank you very much for wtforms.ext.sqlalchemy.fields.QuerySelectField. It saved me a lifetime :)Honky
coerce=int - worked for me, but for other case. thank you!Henna
QuerySelectField() is not working form me. If I use SelectField add coerce=int it works..Size
That's because the latest version (3.0) removes the extension from the main package and makes it a separate package. github.com/wtforms/wtforms-sqlalchemyDitzel
If you include a blank option, then you can use something like this: coerce=lambda value: '' if value == '' else int(value) to avoid ValueError: invalid literal for int() with base 10: ''Overtrade
B
7

Here is how you can solve it without QuerySelectField.

Here is how I did:

years = [(str(y), y) for y in reversed(range(1950, 2013))]
years.insert(0, ('','year'))
year = wt.SelectField(choices=years)
Byron answered 20/11, 2013 at 9:30 Comment(0)
T
6

a 3.01 Pullrequest adds the necessary argument:

 validate_choice=True

To switch off the feature which more often than not seems to be buggy given that 33k views are on this question already and 52 upvotest as of 2022-01.

 SelectField(coerce=int, validate_choice=False) 

made me a happy camper

Timid answered 21/3, 2022 at 19:23 Comment(0)
W
0

Remember to put also the available choices when setting up the Form class.

class MyForm(FlaskForm):
    sel = SelectField(coerce=int, label="MyLabel", choices=my_choices)
    ....

where choices can be something like this:

my_choices = [x.id for x in get_products()]

The exception is arised in function pre_validate in script /wtforms/fields/core.py when the validate_on_submit() functions is called

Wenz answered 16/6, 2021 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.