Form-handling with Formulator and ZPT |
Created by heini1 . Last modified 2005-12-02 12:51:53. |
A small tutorial showing you how to handle forms in a very clean and efficient way using ZPT (Zope Page Template), the Formulator component and Python script. |
In this example we’ll create a form where the user fills in his name, date of birth, e-mail address and chooses his favorite pet. After submitting, the user is directed either to a success page where his name and a thank-you message are displayed or, if some of the data is invalid or missing, to the form page again. In this case, the form-fields are displayed with the data entered by the user and error-messages are displayed in red beside the fields in question.
First of all, we create a Folder in which we’ll put all the
different objects.
<tal: define="global form here/form;
global errors request/error_messages|nothing" />
<p>Please fill in the following form:</p>
<form name="form" method="post" action="form_handle">
<table>
<tr>
<th align="left">name</th>
<td>
<tal:block content="structure python:form.user_name.render_from_request(request.REQUEST)" />
</td>
<td>
<font color="red" tal:condition="python:errors['user_name']"
tal:content="structure python:errors['user_name']"
tal:on-error="string:">
error message
</font>
</td>
</tr>
<tr>
<th align="left">date of birth</th>
<td>
<tal:block content="structure python:form.user_dateofbirth.render_from_request(request.REQUEST)" />
</td>
<td>
<font color="red" tal:condition="python:errors['user_dateofbirth']"
tal:content="structure python:errors['user_dateofbirth']"
tal:on-error="string:">
error message
</font>
</td>
</tr>
<tr>
<th align="left">e-mail address</th>
<td>
<tal:block content="structure python:form.user_email.render_from_request(request.REQUEST)" />
</td>
<td>
<font color="red" tal:condition="python:errors['user_email']"
tal:content="structure python:errors['user_email']"
tal:on-error="string:">
error message
</font>
</td>
</tr>
<tr>
<th align="left">favorite pet</th>
<td>
<tal:block content="structure python:form.user_pet.render_from_request(request.REQUEST)" />
</td>
<td>
<font color="red" tal:condition="python:errors['user_pet']"
tal:content="structure python:errors['user_pet']"
tal:on-error="string:">
error message
</font>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit information" /></td>
<td></td>
</tr>
</table>
</form>
At the beginning of the page we define two variables:
We now create our Formulator object. Add a Formulator Form to your
folder and call it
We now create the page that will be displayed if the form has been
filled in correctly. Create a new Page Template called
Thanks <span tal:replace="options/user">Name</span>, we appreciate your help. This page simply displays a thank-you message containing the users name.
We finally get to the Python script, which will handle the whole
process. Add a Script (Python) to your folder and call it
""" Checks request-data against a formulator object: - if the data is correct, a succes-page is displayed. - if the data is incorrect, a dictionary containing the errors is added to the Request and
First, this script tries to validate the request-data against our
Formulator object. If a FormValidationError is raised, which means
if a field contained invalid data or was missing data, the title of
the field in question (
Ok, here’s how the whole thing works. I hope this small tutorial was comprehensive and helpful for your work. You can download the code of this example here in case I made some mistakes. Othmar Heini |