question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

form_choices "not a valid choice"

See original GitHub issue
    form_choices = {'pref1': [(1, 'Offensive Lineman'), (2, 'Wide Receiver'), (3, 'Offensive Lineman OR Wide Receiver')],
        'pref2': [(1, 'Yes'), (2, 'No'), (3, 'Depends')], 'pref3': [(1, 'Yes'), (2, 'No')],
        'pref4': [(1, 'Yes'), (2, 'No')], 'timezone': [(1, 'EST'), (2, 'CST'), (3, 'PST'), (4, 'Europe')],
        'role': [(2, 'QB'), (1, 'Veteran'), (0, 'Rookie'),]}

They are ints in the database, so I’m not sure why it isn’t working when trying to change any of these form values.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
ahsankhatricommented, May 15, 2019

Well, it can be achieved via form_args property, see below:

First import:

from wtforms.fields import SelectField

Then override field and map your form field:

form_overrides = dict(active=SelectField)
form_args = dict(
    active=dict(choices=[(0, 'No'), (1, 'Yes')], coerce=int)
)

Also, if you want to alias your column in listing then do the following:

column_choices = {'active': [
    (0, 'No'),
    (1, 'Yes'),
]}
2reactions
sampartcommented, Jan 28, 2019

I had this bug as well. It’s because the values coming back from the form are strings (like "1") not integers.

I’m not sure if my solution is the best one - I think there are probably alternatives, but here’s what I did: I overrode the validate_form method to cast the value I needed (to avoid the error). I also had to override the on_form_prefill method to do the reverse conversion - otherwise the selected option wasn’t pre-selected in the drop-down on the edit form.

Here’s an adapted example for a field called pref1. You’d need to extend this to handle all the fields in your code sample, @ToastyBiggums.

class MyView(ModelView):
    # Ensure the pref1 is an int before validating
    # Avoids 'Not a valid choice' error
    def validate_form(self, form):
        form.pref1.data = int(form.status.data)
        return super(MyView, self).validate_form(form)

    # And do the reverse when loading the edit form so the right
    # option is pre-selected
    def on_form_prefill(self, form, id):
        form.pref1.data = str(form.pref1.data)  # in Python 2, use the unicode function instead
Read more comments on GitHub >

github_iconTop Results From Across the Web

WTF.forms "Not a Valid Choice" error for SelectField ...
The problem is that when I submit the form I receive a "Not a valid choice" for these 3 fields. I followed this...
Read more >
Solved: Unable to match form choices to sharepoint list
Solved: I am trying to setup a Flow to pull responses from a MS Form response and post them to a sharepoint list....
Read more >
Form choice field not working, 'Select a valid choice' error
Hi all, when I select an option from my dropdown list I get the following error: Select a valid choice. 94 is not...
Read more >
Common Editing Properties - jqGrid
formoptions (valid only in form editing) ... is a boolean and can have a value of true or false. The option defines whether...
Read more >
[Solved]-Ordering CharField form choices in Django-django
Coding example for the question Ordering CharField form choices in Django-django. ... Error: Select a valid choice. ... is not one of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found