Form Validation with WTForms

When you have to work with form data submitted by a browser view, codequickly becomes very hard to read. There are libraries out there designedto make this process easier to manage. One of them is WTForms which wewill handle here. If you find yourself in the situation of having manyforms, you might want to give it a try.

When you are working with WTForms you have to define your forms as classesfirst. I recommend breaking up the application into multiple modules(Larger Applications) for that and adding a separate module for theforms.

Getting the most out of WTForms with an Extension

The Flask-WTF extension expands on this pattern and adds afew little helpers that make working with forms and Flask morefun. You can get it from PyPI.

The Forms

This is an example form for a typical registration page:

  1. from wtforms import Form, BooleanField, StringField, PasswordField, validators
  2.  
  3. class RegistrationForm(Form):
  4. username = StringField('Username', [validators.Length(min=4, max=25)])
  5. email = StringField('Email Address', [validators.Length(min=6, max=35)])
  6. password = PasswordField('New Password', [
  7. validators.DataRequired(),
  8. validators.EqualTo('confirm', message='Passwords must match')
  9. ])
  10. confirm = PasswordField('Repeat Password')
  11. accept_tos = BooleanField('I accept the TOS', [validators.DataRequired()])

In the View

In the view function, the usage of this form looks like this:

  1. @app.route('/register', methods=['GET', 'POST'])def register(): form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): user = User(form.username.data, form.email.data, form.password.data) db_session.add(user) flash('Thanks for registering') return redirect(url_for('login')) return render_template('register.html', form=form)

Notice we’re implying that the view is using SQLAlchemy here(SQLAlchemy in Flask), but that’s not a requirement, of course. Adaptthe code as necessary.

Things to remember:

  • create the form from the request form value ifthe data is submitted via the HTTP POST method andargs if the data is submitted as GET.

  • to validate the data, call the validate()method, which will return True if the data validates, Falseotherwise.

  • to access individual values from the form, access form..data.

Forms in Templates

Now to the template side. When you pass the form to the templates, you caneasily render them there. Look at the following example template to seehow easy this is. WTForms does half the form generation for us already.To make it even nicer, we can write a macro that renders a field withlabel and a list of errors if there are any.

Here’s an example _formhelpers.html template with such a macro:

  1. {% macro render_field(field) %}
  2. <dt>{{ field.label }}
  3. <dd>{{ field(**kwargs)|safe }}
  4. {% if field.errors %}
  5. <ul class=errors>
  6. {% for error in field.errors %}
  7. <li>{{ error }}</li>
  8. {% endfor %}
  9. </ul>
  10. {% endif %}
  11. </dd>
  12. {% endmacro %}

This macro accepts a couple of keyword arguments that are forwarded toWTForm’s field function, which renders the field for us. The keywordarguments will be inserted as HTML attributes. So, for example, you cancall render_field(form.username, class='username') to add a class tothe input element. Note that WTForms returns standard Python unicodestrings, so we have to tell Jinja2 that this data is already HTML-escapedwith the |safe filter.

Here is the register.html template for the function we used above, whichtakes advantage of the _formhelpers.html template:

  1. {% from "_formhelpers.html" import render_field %}
  2. <form method=post>
  3. <dl>
  4. {{ render_field(form.username) }}
  5. {{ render_field(form.email) }}
  6. {{ render_field(form.password) }}
  7. {{ render_field(form.confirm) }}
  8. {{ render_field(form.accept_tos) }}
  9. </dl>
  10. <p><input type=submit value=Register>
  11. </form>

For more information about WTForms, head over to the WTFormswebsite.