Skip to content Skip to sidebar Skip to footer

Why Is My Flask Form Validation Returning Not A Valid Choice?

I have been trying to figure out why my Flask form will not properly validate my select field choices even though the choices are coming from the select field options. My assumptio

Solution 1:

You have a name mismatch. In the form, you named your select field platforms (plural). In the HTML, you use platform (singular).

I recommend that instead of manually rendering the fields in your template, you let WTForms generate the HTML for you. For the form label, you can use {{ form.platforms.label }}, and for the actual field {{ form.platforms() }}. You can pass any attributes you want to field to have as keyword arguments.

Solution 2:

I think something might be going wrong because of the way you are rendering the form in your html file. If my hunch is right, try this:

{% extends "base.html" %}
{% block content %}
<h4>Select a Platform</h4>
<form method="POST">
    {{ form.hidden_tag() }}
    Select: {{ form.plaforms}}
    {{ form.submit(class="btn btn-default") }}
</form>
{% endblock %}

and then try if form.validate_on_submit() in your views.py file

taken from this stack overflow answer by pjcunningham:

"validate_on_submit() is a shortcut for is_submitted() and validate().

From the source code, line 89, is_submitted() returns True if the form submitted is an active request and the method is POST, PUT, PATCH, or DELETE.

Generally speaking, it is used when a route can accept both GET and POST methods and you want to validate only on a POST request."

Post a Comment for "Why Is My Flask Form Validation Returning Not A Valid Choice?"