Flask-WTF - validate_on_submit() is never executed Flask-WTF - validate_on_submit() is never executed python python

Flask-WTF - validate_on_submit() is never executed


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.


you can print errors

print form.errors

or

app.logger.debug(form.errors)

and if you got csrf-error, you should set form.csrf_token in your template.


I came across this when trying to render a FormField being iterated over my FieldList in my template. I had to embed two hidden_tag elements one for the FieldList form and one for the FieldForm form, search the template comments for keyword "HIDDEN TAG"

class ParamRangeForm( FlaskForm ):    minX = FloatField( )    maxX = FloatField( )class ParamRangesForm( FlaskForm ):    paramRanges = FieldList( FormField( ParamRangeForm ) )    submit      = SubmitField( 'Submit' )    def loadParams( self ) :        for paramName in ["p1" , "p2" , "p3", "p4"] :            prf = ParamRangeForm( )            prf.minX = -100.9#float('-inf')            prf.maxX = 100.5#float('-inf')            self.paramRanges.append_entry( prf )...  <form action="" method="POST" enctype="multipart/form-data">    {{ rangesForm.hidden_tag() }} <!--#### HIDDEN TAG #1 -->    <table>      <!--Print Column Headers-->      <thead>      <tr>        <th class="ColumnHeader">Parameter</td>        <th class="ColumnHeader">Min</td>        <th class="ColumnHeader">Max</td>      </tr>      </thead>      <!--Print Parameter Rows-->      <tbody>      {% for paramRange in rangesForm.paramRanges %}        <tr>          {{ paramRange.hidden_tag() }} <!--#### HIDDEN TAG #2 -->          <td>p{{ loop.index }}</td>          <td>{{ paramRange.minX }}</td>          <td>{{ paramRange.maxX }}</td>        </tr>      {% endfor %}      </tbody>    </table>    </div>    {{ rangesForm.submit() }}  </form>