Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 668
Pavel Vergeev Avatar asked Mar 24 '17 14:03

Pavel Vergeev


People also ask

What is form Validate_on_submit?

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.

What does form Validate_on_submit do flask?

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.


1 Answers

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.

like image 147
pjcunningham Avatar answered Dec 31 '22 13:12

pjcunningham