Not a Valid Choice for Dynamic Select Field WTFORMS

Question:

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.

Asked By: ismail

||

Answers:

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)
Answered By: Sean Vieira

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)
Answered By: Rickard Zachrisson

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

Answered By: Edoardo Vignati

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

Answered By: Wolfgang Fahl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.