Difference between `form.validate_on_submit()` and

2019-02-18 09:43发布

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.

1条回答
倾城 Initia
2楼-- · 2019-02-18 09:44

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.

查看更多
登录 后发表回答