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.
The validate_on_submit() method of the form returns True when the form was submitted and the data was accepted by all the field validators. In all other cases, validate_on_submit() returns False . The return value of this method effectively serves to determine whether the form needs to be rendered or processed.
Validating Forms Note that you don't have to pass request. form to Flask-WTF; it will load automatically. And the convenient validate_on_submit will check if it is a POST request and if it is valid. If your forms include validation, you'll need to add to your template to display any error messages.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With