Form Variable Types and Typechecking
Created by .
Last modified on 2003/08/01.
Most of this information was stolen from the old
Trinkets Tutorial
by Jim Fulton (Digital Creations),
which will probably disappear when the old Zope.org site
is no more, so I'm preserving some still relevant sections
here for posterity.
Form variable types
Normally, Zope copies values from form fields as strings. Zope can be told to perform type conversions automatically by including type information in form variable names.
In the form generated by the downloadForm
method, the input tag for the hidden field, secret, has a name attribute of secret:int
. The :int
part of the name indicates to Zope that the field should be converted to an integer before passing it to the published method.
Currently supported type conversions
Type | Python Object Passed |
float |
Float |
int |
Integer |
long |
Long integer |
string |
String |
required |
Non-blank string |
text |
String with carriage-return newline pairs converted to newlines. |
lines |
List of values separated by newlines. |
tokens |
List of values entered as multiple space-separated tokens in a single field. |
date |
Date Time instance |
list |
List |
tuple |
Tuple |
record |
Record |
ignore_empty |
Ignore empty |
default |
default |
records | List of records |
Examples of some supported types
RECORD
<INPUT TYPE="text" NAME="person.name:record" SIZE="30">
The input tag for the name
field is named person.name:record
. The :record
part of the name indicates to Zope that the field should
be converted to a record before passing it to the published method. A record variable will be created called person
and it will have an attribute
called name
.
IGNORE_EMPTY
<INPUT TYPE="text" NAME="person.email:record:ignore_empty" SIZE="30">
When the email form field is left blank, :ignore_empty
skips over the variable rather than returning a null string as its value.
When the record person
is returned it will not have an attribute called email
if the user did not enter one.
DEFAULT
<INPUT TYPE="hidden" NAME="person.toppings:record:list:default" VALUE="All">
<select multiple NAME = "person.toppings:record:list:ignore_empty">
<option>Bacon</option>
<option>Cheese</option>
<option>Tomato</option>
<option>Pepper</option>
<option>Egg<option>
</select>
The default type allows a specified value to be inserted when the form field is left blank. In the above example, if the user does
not select values from the list of toppings, the default value will be used. The record person
will have the attribute toppings and
its value will be the list containing the word All (if the field is empty) or a list containing the selected toppings.
RECORDS
<P><H2>Member #1<H2></P><BR>
<P>Please enter your name:<BR>
<INPUT TYPE="text" NAME="member.name:records:ignore_empty" SIZE="30"><BR>
your email:<BR>
<INPUT TYPE="text" NAME="member.email:records:ignore_empty" SIZE="30"><BR>
your age:<BR>
<INPUT TYPE="hidden" NAME="member.age:int:records:default" Value="0"<BR>
<INPUT TYPE="text" NAME="member.age:int:records:ignore_empty"></P><BR>
<P><H2>Member #2</H2></P><BR>
<P>Please enter your name:<BR>
<INPUT TYPE="text" NAME="member.name:records:ignore_empty" SIZE="30"><BR>
your email:<BR>
<INPUT TYPE="text" NAME="member.email:records:ignore_empty" SIZE="30"><BR>
your age:<BR>
<INPUT TYPE="text" NAME="member.age:int:records:ignore_empty"><BR>
</P>
In this sample the form asks for a name and age for two different
members of a club. The :records
part appears in each input tag.
This type will create a list called members
that will hold records
(in this case it will hold two records). Each record can have the
attributes name, email and age.
Note:
Before creating a second record and adding an attribute, Zope gets the last record in the list and checks to see if
that record has the attribute. If the first record doesn't have the
attribute, it inserts the attribute and value instead of creating a new
record. For example, if the user entered a name and age for Member
#1 and only an email for Member #2, the list will have only one record.
That record will contain the name and age attribute from Member #1 as well
as the email attribute from Member #2.
Type checking
If a value entered in a form does not match a type specification, or if a form value is omitted, Zope automatically generates an error response.
This frees the programmer from writing some error checking code.
Note that as of Zope 2.0.1, this error response is your
standard Zope error/traceback screen, so I wouldn't recommend
using it to enforce numeric or date types unless you combine
it with fancy JavaScript or just don't care.