Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-WTF - validate_on_submit() is never executed

I'm using Flask-WTF:

Here is my form:

from flask.ext.wtf import Form, TextField  class BookNewForm(Form):     name = TextField('Name') 

Here is the controller:

@book.route('/book/new', methods=['GET', 'POST']) def customers_new():     form = BookNewForm()     if form.is_submitted():         print "submitted"     if form.validate():         print "valid"     if form.validate_on_submit():         flash("Successfully created a new book")         return redirect(url_for('.books_show'))     return render_template('views/books_new.html', form=form) 

Now the problem is, if you look at my print statements, it always prints submitted, but it NEVER prints valid and validate_on_submit() is never executed. Why?

like image 850
kadrian Avatar asked May 23 '12 15:05

kadrian


People also ask

What is Validate_on_submit?

The call to validate_on_submit() invokes the DataRequired() validator attached to the name field. If the name is not empty, then the validator accepts it and validate_on_submit() returns True . Now the name entered by the user is accessible as the data attribute of the field.

What is WTForms in Python?

WTForms is a Python library that provides flexible web form rendering. You can use it to render text fields, text areas, password fields, radio buttons, and others. WTForms also provides powerful data validation using different validators, which validate that the data the user submits meets certain criteria you define.

Which of the following validators can be used to compare values of two form fields?

You can perform this type of form validation by using the CompareValidator control. To compare two dates, you need to set the ControlToValidate, ControlToCompare, Operator, and Type properties of the CompareValidator control.


1 Answers

You're not inserting the CSRF field in the HTML form.

<form method=post>     {{ form.csrf_token }}     {{ form.name }}     <input type=submit> </form> 

After adding form.csrf_token to the template (docs), the form will validate as expected.

Add print(form.errors) after validating the form to see the errors that were raised. errors will be empty before validation. In this case, there is an error about missing

@book.route('/book/new_no_csrf', methods=['GET', 'POST']) def customers_new_no_csrf():     form = BookNewForm()     print(form.errors)      if form.is_submitted():         print "submitted"      if form.validate():         print "valid"      print(form.errors)      if form.validate_on_submit():         flash("Successfully created a new book")         return redirect(url_for('.books_show'))      return render_template('books_new.html', form=form) 
{} submitted {'csrf_token': [u'CSRF token missing']} 127.0.0.1 - - [29/May/2012 02:01:08] "POST /book/new_no_csrf HTTP/1.1" 200 - 127.0.0.1 - - [29/May/2012 02:01:08] "GET /favicon.ico HTTP/1.1" 404 - 

I created an example on GitHub.

like image 179
A.Ford Avatar answered Sep 22 '22 17:09

A.Ford