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

zine::utils::forms::ChoiceField Class Reference

Inheritance diagram for zine::utils::forms::ChoiceField:

zine::utils::forms::Field zine::utils::forms::MultiChoiceField

List of all members.

Detailed Description

A field that lets a user select one out of many choices.

A choice field accepts some choices that are valid values for it.
Values are compared after converting to unicode which means that
``1 == "1"``:

>>> field = ChoiceField(choices=[1, 2, 3])
>>> field('1')
>>> field('42')
Traceback (most recent call last):
ValidationError: Please enter a valid choice.

Two values `a` and `b` are considered equal if either ``a == b`` or
``primitive(a) == primitive(b)`` where `primitive` is the primitive
of the value.  Primitives are created with the following algorithm:

    1.  if the object is `None` the primitive is the empty string
    2.  otherwise the primitive is the string value of the object

A choice field also accepts lists of tuples as argument where the
first item is used for comparing and the second for displaying
(which is used by the `SelectBoxWidget`):

>>> field = ChoiceField(choices=[(0, 'inactive'), (1, 'active')])
>>> field('0')

Because all fields are bound to the form before validation it's
possible to assign the choices later:

>>> class MyForm(Form):
...     status = ChoiceField()
>>> form = MyForm()
>>> form.status.choices = [(0, 'inactive', 1, 'active')]
>>> form.validate({'status': '0'})
>>> form.data
{'status': 0}

If a choice field is set to "not required" and a `SelectBox` is used
as widget you have to provide an empty choice or the field cannot be
left blank.

>>> field = ChoiceField(required=False, choices=[('', _('Nothing')),
...                                              ('1', _('Something'))])

Definition at line 1410 of file forms.py.

Public Member Functions

def __call__
def __copy__
def __init__
def __init__
def __repr__
def apply_validators
def bound
def convert
def get_default
def should_validate
def to_primitive

Public Attributes


Static Public Attributes

 form = None
tuple messages
 multiple_choices = False
 validate_on_omission = False
 widget = SelectBox

Private Member Functions

def _bind

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

Generated by  Doxygen 1.6.0   Back to index