| 1 |
.. _forms: |
| 2 |
|
| 3 |
Generating and Validating Forms |
| 4 |
=============================== |
| 5 |
|
| 6 |
Pecan provides no opinionated support for working with |
| 7 |
form generation and validation libraries, but it’s easy to import your |
| 8 |
library of choice with minimal effort. |
| 9 |
|
| 10 |
This article details best practices for integrating the popular forms library, |
| 11 |
`WTForms <http://wtforms.simplecodes.com/>`_, into your Pecan project. |
| 12 |
|
| 13 |
Defining a Form Definition |
| 14 |
-------------------------- |
| 15 |
|
| 16 |
Let's start by building a basic form with a required ``first_name`` |
| 17 |
field and an optional ``last_name`` field. |
| 18 |
|
| 19 |
:: |
| 20 |
|
| 21 |
from wtforms import Form, TextField, validators |
| 22 |
|
| 23 |
class MyForm(Form): |
| 24 |
first_name = TextField(u'First Name', validators=[validators.required()]) |
| 25 |
last_name = TextField(u'Last Name', validators=[validators.optional()]) |
| 26 |
|
| 27 |
class SomeController(object): |
| 28 |
pass |
| 29 |
|
| 30 |
Rendering a Form in a Template |
| 31 |
------------------------------ |
| 32 |
|
| 33 |
Next, let's add a controller, and pass a form instance to the template. |
| 34 |
|
| 35 |
:: |
| 36 |
|
| 37 |
from pecan import expose |
| 38 |
from wtforms import Form, TextField, validators |
| 39 |
|
| 40 |
class MyForm(Form): |
| 41 |
first_name = TextField(u'First Name', validators=[validators.required()]) |
| 42 |
last_name = TextField(u'Last Name', validators=[validators.optional()]) |
| 43 |
|
| 44 |
class SomeController(object): |
| 45 |
|
| 46 |
@expose(template='index.html') |
| 47 |
def index(self): |
| 48 |
return dict(form=MyForm()) |
| 49 |
|
| 50 |
Here's the Mako_ template file: |
| 51 |
|
| 52 |
.. _Mako: http://www.makeotemplates.org/ |
| 53 |
|
| 54 |
.. code-block:: html |
| 55 |
|
| 56 |
<form method="post" action="/"> |
| 57 |
<div> |
| 58 |
${form.first_name.label}: |
| 59 |
${form.first_name} |
| 60 |
</div> |
| 61 |
<div> |
| 62 |
${form.last_name.label}: |
| 63 |
${form.last_name} |
| 64 |
</div> |
| 65 |
<input type="submit" value="submit"> |
| 66 |
</form> |
| 67 |
|
| 68 |
Validating POST Values |
| 69 |
---------------------- |
| 70 |
|
| 71 |
Using the same :class:`MyForm` definition, let's redirect the user if the form is |
| 72 |
validated, otherwise, render the form again. |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
from pecan import expose, request |
| 77 |
from wtforms import Form, TextField, validators |
| 78 |
|
| 79 |
class MyForm(Form): |
| 80 |
first_name = TextField(u'First Name', validators=[validators.required()]) |
| 81 |
last_name = TextField(u'Last Name', validators=[validators.optional()]) |
| 82 |
|
| 83 |
class SomeController(object): |
| 84 |
|
| 85 |
@expose(template='index.html') |
| 86 |
def index(self): |
| 87 |
my_form = MyForm(request.POST) |
| 88 |
if request.method == 'POST' and my_form.validate(): |
| 89 |
# save_values() |
| 90 |
redirect('/success') |
| 91 |
else: |
| 92 |
return dict(form=my_form) |