Logo Search packages:      
Sourcecode: zine version File versions  Download package

zine::utils::forms::Form Class Reference

List of all members.

Detailed Description

Form base class.

>>> class PersonForm(Form):
...     name = TextField(required=True)
...     age = IntegerField()

>>> form = PersonForm()
>>> form.validate({'name': 'johnny', 'age': '42'})
>>> form.data['name']
>>> form.data['age']

Let's cause a simple validation error:

>>> form = PersonForm()
>>> form.validate({'name': '', 'age': 'fourty-two'})
>>> print form.errors['age'][0]
Please enter a whole number.
>>> print form.errors['name'][0]
This field is required.

You can also add custom validation routines for fields by adding methods
that start with the prefix ``validate_`` and the field name that take the
value as argument. For example:

>>> class PersonForm(Form):
...     name = TextField(required=True)
...     age = IntegerField()
...     def validate_name(self, value):
...         if not value.isalpha():
...             raise ValidationError(u'The value must only contain letters')

>>> form = PersonForm()
>>> form.validate({'name': 'mr.t', 'age': '42'})
>>> form.errors
{'name': [u'The value must only contain letters']}

You can also validate multiple fields in the context of other fields.
That validation is performed after all other validations.  Just add a
method called ``context_validate`` that is passed the dict of all fields::

>>> class RegisterForm(Form):
...     username = TextField(required=True)
...     password = TextField(required=True)
...     password_again = TextField(required=True)
...     def context_validate(self, data):
...         if data['password'] != data['password_again']:
...             raise ValidationError(u'The two passwords must be the same')

>>> form = RegisterForm()
>>> form.validate({'username': 'admin', 'password': 'blah',
...                'password_again': 'blag'})
>>> form.errors
{None: [u'The two passwords must be the same']}

Forms can be used as fields for other forms.  To create a form field of
a form you can call the `as_field` class method::

>>> field = RegisterForm.as_field()

This field can be used like any other field class.  What's important about
forms as fields is that validators don't get an instance of `RegisterForm`
passed as `form` / `self` but the form where it's used in if the field is
used from a form.

Form fields are bound to the form on form instanciation.  This makes it
possible to modify a particular instance of the form.  For example you
can create an instance of it and drop some fiels by using
``del form.fields['name']`` or reassign choices of choice fields.  It's
however not easily possible to add new fields to an instance because newly
added fields wouldn't be bound.  The fields that are stored directly on
the form can also be accessed with their name like a regular attribute.

Example usage:

>>> class StatusForm(Form):
...     status = ChoiceField()
>>> StatusForm.status.bound
>>> form = StatusForm()
>>> form.status.bound
>>> form.status.choices = [u'happy', u'unhappy']
>>> form.validate({'status': u'happy'})
>>> form['status']

Fields support default values.  These however are not as useful as you
might think.  These defaults are just annotations for external handling.
The form validation system does not respect those values.

They are for example used in the configuration system.


>>> field = TextField(default=u'foo')

Definition at line 1711 of file forms.py.

Public Member Functions

def __contains__
def __getitem__
def __init__
def add_invalid_redirect_target
def as_widget
def csrf_token
def fields
def has_changed
def is_valid
def redirect
def redirect_target
def reset
def validate
def validators

Public Attributes


Static Public Attributes

 csrf_protected = True
 redirect_tracking = True

Private Attributes


Static Private Attributes

 __metaclass__ = FormMeta

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index