Difference between `form.validate_on_submit()` and `form.validate()`

Pavel Vergeev picture Pavel Vergeev · Mar 24, 2017 · Viewed 7.7k times · Source

What is the difference between form.validate_on_submit() and form.validate()?

In the Flask WTF docs, the author uses form.validate_on_submit(). The code works. When I substitute form.validate_on_submit() with form.validate(), I see no difference in behavior. I couldn't find form.validate_on_submit() in the source, but I managed to find form.validate() code.

Answer

pjcunningham picture pjcunningham · Mar 24, 2017

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.