summaryrefslogtreecommitdiff
path: root/docs/source/forms.rst
blob: 91c3f062d512ac20889d640f006443442ddad90f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
.. _forms:

Generating and Validating Forms
===============================
Out of the box, Pecan provides no opinionated support for working with
form generation and validation libraries, but it’s easy to import your
library of choice with minimal effort.

This article details best practices for integrating the popular forms library,
`WTForms <http://wtforms.simplecodes.com/>`_, into your Pecan project.

Defining a Form Definition
--------------------------

Let's start by building a basic form with a required ``first_name`` field and an optional ``last_name`` field::

    from wtforms import Form, TextField, validators

    class MyForm(Form):
        first_name = TextField(u'First Name', validators=[validators.required()])
        last_name = TextField(u'Last Name', validators=[validators.optional()])

    class SomeController(object):
        pass

Rendering a Form in a Template
------------------------------

Next, let's add a controller, and pass a form instance to the template::

    from pecan import expose
    from wtforms import Form, TextField, validators

    class MyForm(Form):
        first_name = TextField(u'First Name', validators=[validators.required()])
        last_name = TextField(u'Last Name', validators=[validators.optional()])

    class SomeController(object):

        @expose(template='index.html')
        def index(self):
            return dict(form=MyForm())

Here's the template file (which uses the `Mako <http://www.makeotemplates.org/>`_
templating language):

.. code-block:: html

    <form method="post" action="/">
        <div>
            ${form.first_name.label}:
            ${form.first_name}
        </div>
        <div>
            ${form.last_name.label}:
            ${form.last_name}
        </div>
        <input type="submit" value="submit">
    </form>

Validating POST Values
----------------------

Using the same ``MyForm`` definition, let's redirect the user if the form is 
validated, otherwise, render the form again.

.. code-block:: python

    from pecan import expose, request
    from wtforms import Form, TextField, validators

    class MyForm(Form):
        first_name = TextField(u'First Name', validators=[validators.required()])
        last_name = TextField(u'Last Name', validators=[validators.optional()])

    class SomeController(object):

        @expose(template='index.html')
        def index(self):
            my_form = MyForm(request.POST)
            if request.method == 'POST' and my_form.validate():
                # save_values()
                redirect('/success')
            else:
                return dict(form=my_form)