Chapter 17.5.2. validate()

The validate() decorator validates input, and if it is valid, converts it into corresponding Python objects. These two steps can be performed on both the individual arguments to your controller methods and complete forms (see Chapter 16, “TurboGears Widgets,” for more detail).

For individual arguments, you can easily set up validators for individual keyword arguments by setting up a dictionary where the keys are the names of the arguments, and the values are the validators you would like used on each argument. Here’s a sample:

@validate(validators={"num":validators.Int(),
         "content":validators.NotEmpty()})
def page(self, num, content=None):

Note that validation is not limited to keyword arguments.

Validating forms ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.