Tornado Tools

Form validation

«  Mongrel2 handler for Tornado   ::   Contents   ::   Caching asynchronous methods  »

Form validation

A simple wrapper for WTForms.

Basically we only need to map the request handler’s arguments to the wtforms.form.Form input. Quick example:

from wtforms import TextField, validators
from tornadotools.forms import Form

class SampleForm(Form):
    username = TextField('Username', [
        validators.Length(min=4, message="Too short")
        ])

    email = TextField('Email', [
        validators.Length(min=4, message="Not a valid mail address"),
        validators.Email()
        ])

Then, in the RequestHandler:

def get(self):
    form = SampleForm(self)
    if form.validate():
        # do something with form.username or form.email
        pass
    self.render('template.html', form=form)

«  Mongrel2 handler for Tornado   ::   Contents   ::   Caching asynchronous methods  »