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

'Not a valid choice' error when submitting option added to SelectField with Javascript #434

Closed
ghost opened this issue Jul 19, 2018 · 8 comments · Fixed by #493
Closed

Comments

@ghost
Copy link

@ghost ghost commented Jul 19, 2018

Problem

If a form is submitted and the value of a SelectField is an option that was added by Javascript, WTForms will give a 'Not a valid choice' error message.

Proposal

Add a keyword argument called validate_choice to SelectField's __init__ method that defaults to True. If it is False, WTForms will not check that the selected option is in the field's choices attribute.

@whb07
Copy link
Contributor

@whb07 whb07 commented Jul 25, 2018

Hi can you show some code or perhaps an example where you see this? What is the way you're validating once the POST request comes to the backend?

@hiphamster
Copy link

@hiphamster hiphamster commented Jun 6, 2019

I just ran into exactly the same issue.
in my form class i declare a SelectField like this:
site = SelectField('site', choices=[])
later on, i set values via javascript using selectize.js
on submit i get 'Form validation failed', 'site: Not a valid choice'

@hiphamster
Copy link

@hiphamster hiphamster commented Jun 6, 2019

I just found a workaround. I followed the accepted answer, and created a new NonValidatingSelectField class:

class NonValidatingSelectField(SelectField):
    def pre_validate(self, form):
        pass 

But this is not obvious. It'd be nice to be able to pass a validation flag to the field, something like:
site = SelectField('site', choices=[], validate=False)

@ftm
Copy link
Contributor

@ftm ftm commented Jun 12, 2019

This seems like a reasonable feature to add, I'll write a PR up for it

@ftm ftm added enhancement and removed enhancement labels Jun 12, 2019
@ftm ftm closed this as completed in #493 Jun 12, 2019
@tvb
Copy link

@tvb tvb commented Mar 21, 2020

@ftm I know this is an old thread, but I have the same issues with the SelectMultipleField and would like to set the validate_choice to False too. Any idea?

@ftm
Copy link
Contributor

@ftm ftm commented Mar 31, 2020

@ftm I know this is an old thread, but I have the same issues with the SelectMultipleField and would like to set the validate_choice to False too. Any idea?

@tvb I did not add the validate_choice option to SelectMultipleField which was probably a mistake. If a PR is submitted to add the option then I'd be happy to accept it.

@wtforms wtforms deleted a comment from lifeisawavesorideit Aug 10, 2020
@caffeinatedMike
Copy link

@caffeinatedMike caffeinatedMike commented Sep 14, 2020

@hiphamster's seems to be the best option currently for SelectMultipleFields. I see the validate_choice option on the field, but it appears that it's completely overlooked during the validation process; as is evident when providing values via javascript and walking through the validation code upon calling form.validate_on_submit(). Was the feature started and just overlooked/never finished?

@AlverezYari
Copy link

@AlverezYari AlverezYari commented May 18, 2021

Just ran into this one myself. Any new updates on that PR? Was it ever started?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

6 participants