File: Form.tex

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (296 lines) | stat: -rwxr-xr-x 12,736 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
\section{\module{Form} --- Form handling.}
\declaremodule{standard}{Form}

\subsection{Introduction}
Handling complicated forms can really be a pain sometimes, especially if you want to handle user errors.

The Form module can save you a lot of time and trouble, once you've learned how to use it.

Most of the time, you'll want this:
\begin{itemize}
\item
Your form has all sorts of fields: text fields, textareas, checkboxes, radio buttons, ...
\item
By default, some fields are empty, and some have default values.
\item
Some fields are mandatory, some aren't. Some fields can only have certain values (ex: birthdate, price, ...)
\end{itemize}

And you'll probably want your form to behave like this:
\begin{itemize}
\item
When the form is first displayed, all fields are either empty or they have a default value
\item
The user fills the form in and hit the submit button
\item
(At this point, you may want to use a few lines of javascript to catch trivial errors. But if your
form is really big, you'll probably want to catch these errors on the server side ...)
\item
The data is sent to the server, which analyzes it
\item
If the data is correct (no missing field, no wrong value, ...), everything continues normally
\item
In case some fields have incorrect values, you'll probably want the following:
\begin{itemize}
\item
Redisplay the form, but keep all values that the user entered (that's the painful part ...)
\item
Display a message that stands out at the top of the form to notify the user that some fields need to be changed
\item
Display a message next to each field that has an error
\end{itemize}
\end{itemize}

We'll see how the Form module can help you do that ...

\subsection{Module}

This module defines 4 CherryClasses:

\subsubsection{FormField}
A FormField instance is used for each of the form fields.

\begin{funcdesc}{function: __init__}{label, name, typ, mask=None, mandatory=0, size=15, optionList=[], defaultValue='', validate=None}

\var{label} is a string that will be displayed next to the field.

\var{name} is a string containing the name of the field.

\var{typ} is a string containing the type of the field. It can be one of the following:
text, password, file, hidden, submit, image, select, textarea, radio, checkbox

\var{mask} is a mask used to render the field. The default value is \var{defaultFormMask.defaultMask}. The mask will receive
the FormField instance as an argument and it should return some HTML to render the field.

\var{mandatory} is an integer that indicates whether the field is mandatory or not.

\var{size} is an integer that indicates the size of the field.

\var{mandatory} is an integer that indicates whether the field is mandatory or not (it is only used for some of the fields
like text or password).

\var{optionList} is a list of strings containing the different options for a fied (is is only used for
radio and checkbox fields).

\var{defaultValue} is a string containing the default value for the field.

\var{validate} is a function used to validate the field. The function will receive the value of the field as an argument,
and it should return \var{None} if the value is correct, or a string containing the error message if the value is not.

\end{funcdesc}

\subsubsection{FormSeparator}
A FormSeparator instance is used to display some text or images between the different fields of the form.

\begin{funcdesc}{function: __init__}{label, mask}

\var{label} is a string that will be used by the mask to know what to display.

\var{mask} is a mask used to render the field. The mask will receive
the FormSeparator instance as an argument and it should return some HTML to render the separator.
\end{funcdesc}

\subsubsection{DefaultFormMask}
This CherryClass contains a default implementation of a mask for the fields. You'll probably want to use
your own masks for your own design. The next section explains how to write your own field masks.

\subsubsection{Form}

The is the main CherryClass of the module. To create a form, you should declare a CherryClass that inherits from Form.

You may use the following variables and methods:

\begin{memberdesc}{variable: method}
String containing the \var{method} attribute of the form tag. It may be \var{send} or \var{post}. The default value
is \var{post}
\end{memberdesc}
\begin{memberdesc}{variable: enctype}
String containing the \var{enctype} attribute of the form tag. For instance, for a form that allows the user to upload
files, you would use \var{multipart/form-data} The default value is an empty string, which means that the \var{enctype}
attribute wile be omitted.
\end{memberdesc}
\begin{memberdesc}{variable: fieldList}
List containing instances of the FormField and FormInstance CherryClasses. This list determines which fields and
separators will be displayed, and in which order. \var{fieldList} should be set in the \var{__init__} method of the
CherryClass.
\end{memberdesc}

\begin{funcdesc}{function: formView}{leaveValues=0}
This function returns the HTML code for the form. if \var{leaveValues} is false, it will use the default value
for each of the fields. If \var{leaveValues} is true, it will use the values that are in \var{request.paramMap} (in other
words, the values that were entered by the user)
\end{funcdesc}

\begin{funcdesc}{function: validateFields}{}
This function should be overwritten if you need to perform some validation that involves several fields at the
same time (for instance, checking that 2 passwords match).

If a field has an error, the function should set the \var{errorMessage} member variable of the FormField instance.
\end{funcdesc}

\begin{funcdesc}{function: setFieldErrorMessage}{fieldName, errorMessage}
Sets the \var{errorMessage} member variable of the FormField instance whose name is \var{fieldName}.
\end{funcdesc}

\begin{funcdesc}{function: getFieldOptionList}{fieldName}
Returns the \var{optionList} member variable of the FormField instance whose name is \var{fieldName}.
\end{funcdesc}

\begin{funcdesc}{function: getFieldDefaultValue}{fieldName}
Returns the \var{defaultValue} member variable of the FormField instance whose name is \var{fieldName}.
\end{funcdesc}

\begin{funcdesc}{function: setFieldDefaultValue}{fieldName, defaultValue}
Sets the \var{defaultValue} member variable of the FormField instance whose name is \var{fieldName}.
\end{funcdesc}

\begin{funcdesc}{function: getFieldNameList}{exceptList=[]}
Returns the list of field names, based on the \var{fieldList} member variable. Names that are in \var{exceptList} are omitted.
\end{funcdesc}

\begin{funcdesc}{function: validateForm}{}
This function checks if the data that the user entered is correct or not. It returns 1 if it is, 0 otherwise.
\end{funcdesc}

\begin{funcdesc}{view: postForm}{**kw}
This view is automatically called when the user submits the form. You should overwrite this view and add your own
code to handle the form data. Typical code for this view looks like this:
\begin{verbatim}
def postForm(self, **kw):
    if self.validateForm():
        # Yes, the data is correct
        # Do what you want here
        pass
    else:
        # No, the data is incorrect
        # Redisplay the form and tell the user to fix the errors:
        return "<html><body><font color=red>Fill out missing fields</font>"+self.formView(1)+"</body></html>"
\end{verbatim}
\end{funcdesc}

\subsection{Writing a form mask}
The module comes with a default mask for forms, but you'll probably want to change it to use your own design.
All you have to do is write your own form mask.

A form mask takes a FormField instance as an input and returns some HTML code as output. Don't forget that your
mask should be setting the value of the field according the the \var{currentValue} member variable. Moreover,
it should handle the field differently if the \var{errorMessage} is set.

For instance, a mask for a text field could look like this:
\begin{verbatim}
if field.typ=='text':
    result='%s: <input type=text name="%s" value="%s" size="%s">'%(
        field.label, field.name, field.currentValue, field.size)
    if field.errorMessage:
        result+=' <font color=red>%s</font>'%field.errorMessage
return result+'<br>'
\end{verbatim}

Things are a bit trickier for select boxes, radio buttons or checkboxes because you have to loop over
the \var{optionList} member variable and match each value against \var{currentValue}.

For instance, for a select box, the mask could look like this:
\begin{verbatim}
if field.typ=='select':
    result='%s: <select name="%s" size="%s">'%(field.label, field.name, field.size)
    for optionValue in optionList:
        if optionValue==field.currentValue: checked=' checked'
        else: checked=''
        result+='<option%s>%s</option>'%(checked,optionValue)
    result+='</select>
    if field.errorMessage:
        result+=' <font color=red>%s</font>'%field.errorMessage
return result+'<br>'
\end{verbatim}


\subsection{Putting it together}

Let's see how we use all these CherryClasses, variables and methods to build a nice form.

We are going to build a form where users choose a login and a password, enter their e-mail, their country and their
hobbies.

We need 6 fields:
\begin{itemize}
\item
One text field for the login (this field is mandatory)
\item
Two password fields for the password (which they must enter twice)
\item
One text field for their e-mail (this field is optional)
\item
One select field for their country (the default value is USA)
\item
One checkbox list for their hobbies (this field is optional)
\end{itemize}

Plus we'll add one line between the e-mail field and the country field.

Here is what the code could be:
\begin{verbatim}
use Form, MaskTools

# We start by creating a CherryClass that inherits from Form
# This CherryClass will hold all the informations about the form we want to create
CherryClass MyForm(Form):
function:
    def __init__(self):
        # Instantiate all fields plus 3 separators (one at the beginning, one for the line and one at the end)
        headerSep=FormSeparator('', defaultFormMask.defaultHeader)
        login=FormField(label='Login:', name='login', mandatory=1, typ='text')
        password=FormField(label='Password:', name='password', mandatory=1, typ='password')
        password2=FormField(label='Confirm password:', name='password2', mandatory=1, typ='password')
        email=FormField(label='E-mail:', name='email', typ='text', validate=self.validateEmail)
        lineSep=FormSeparator('', self.lineSeparator)
        country=FormField(label='Country:', name='country', typ='select', optionList=['USA', 'Andorra', 'Lichtenstein', 'CherryPyLand'], defaultValue='USA')
        hobbies=FormField(label='Hobbies:', name='hobbies', typ='checkbox', optionList=['Using CherryPy', 'Eating Cherry Pie'])
        submit=FormField(label='', name='Submit', typ='submit')
        footerSep=FormSeparator('', defaultFormMask.defaultFooter)
        self.fieldList=[headerSep, login, password, password2, email, lineSep, country, hobbies, submit, footerSep]

    # Function that checks if an e-mail is correct or not
    def validateEmail(self, email):
        try:
            before, after=email.split('@')
            if not before or after.find('.')==-1: raise 'Error'
        except: return "Wrong email"

    # Function that performs general validation of the form. In our case, we need to check
    # that the passwords match
    def validateFields(self):
        # Warning: paramMap could have no "password" or "password2" key if the user didn't fill out the fields
        if request.paramMap.get('password','')!=request.paramMap.get('password2',''):
            # Set errorMessage for password fields
            self.setFieldErrorMessage('password', 'Not matching')
            self.setFieldErrorMessage('password2', 'Not matching')

mask:
    # Line separator used to draw a line between the email field and the country field
    def lineSeparator(self, label):
        <tr><td colspan=3 height=1 bgColor=black py-eval="maskTools.x()"></td></tr>

view:
    def postForm(self, **kw):
        if self.validateForm():
            return root.formOk()
        else:
            return "<html><body><font color=red>Please correct the errors (fields in red)</font>"+self.formView(1)+"</body></html>"


# Now we just have to create a regular Root CherryClass, that will call some of MyForm's methods
CherryClass Root:
mask:
    def index(self):
        <html><body>
            Welcome, please fill out the form below:
            <py-eval="myForm.formView()">
        </body></html>
    def formOk(self):
        <html><body>
            Thank you for filling out the form.<br>
            All values were correct
        </body></html>
\end{verbatim}