user455318
user455318

Reputation: 3346

Validate get when submitting with flask wtforms

I have a form with validations, like required() or NumberRange.

This is a search functionality, so I am using get method. The problem is that I can't use form.validate_on_submit() with get. With post everything work as expected, but with get the form ignore the validations, basically never validate on submit.

What is the best way to handle this situation?

class DocSearch(Form):
    client_number = IntegerField(lazy_gettext(u'Client number'), [Required()])
    submit = SubmitField(lazy_gettext(u'Find'))

<form method="get" class="form" role="form">
    <div class="form-group ">
        {{ render_field(form.client_number) }}
    </div>

    <div class="form-group ">
        {{ render_field(form.submit) }}
    </div>
</form>

Upvotes: 0

Views: 350

Answers (1)

dirn
dirn

Reputation: 20729

validate_on_submit is a convenience function that calls two other methods. The first, is_submitted, checks for POST and PUT requests. The second, validate, does the actual validation. That's the one you need.

if form.validate():
    # do stuff here

Upvotes: 1

Related Questions