All fields derive from the Field class. A field has 2 methods: Field.validate() and Field.convert(). validate is called first, and if the value is valid, convert is called.
If required is True, the field has to be present and non-empty. default is the value returned if the field is empty or absent.
id and label are the tag’s id and its label text. For example:
Field(id='field1', label='The First Field')
Its tag will look like:
<... id='field1' ...>
And its label will be:
<label for='field1'>The First Field</label>
Convert value into its Python equivalent. By default it just return the passed value.
Return an error if the field is invalid. If the field is valid it doesn’t return anything:
>>> f = Field(required=True)
>>> f.validate('something')
>>> f.validate('')
u'Required'
It can also raise a StopValidation exception. In this case the arguments are used as errors:
raise StopValidation(u'Some error')
u'Some error' will be added in the error list.
You can specify its minimum and maximum length with min_length and max_length:
>>> s = String(min_length=2, max_length=4)
>>> s.validate('123')
>>> s.validate('too long')
u'Must be less than 4 characters long'
>>> s.validate('x')
u'Must be at least 2 characters long'
An integer field. The integer conversion is done by the builtin method int(). base is the number’s base:
>>> Integer().validate('123')
>>> Integer().validate('bad number')
u'Invalid number'
>>> Integer().convert('123')
123
Note that you can use prefixes for the octal and hexadecimal bases:
>>> Integer(base=8).convert('0123')
83
>>> Integer(base=16).convert('0x123')
291
min and max can be used to set the limits of the integer. Those parameters are inclusive. This means that max=100 will accept 100 as a valid value:
>>> Integer(max=10).validate('10')
>>> Integer(max=10).validate('11')
u'Must be less than 11'
If the field is empty or absent, it is false. Otherwise it’s true. This field always validate:
>>> Boolean().validate(None)
>>> Boolean().validate('')
>>> Boolean().validate('xxx')
An empty value or None is false:
>>> Boolean().convert(None)
False
>>> Boolean().convert('')
False
>>> Boolean().convert('on')
True
>>> Boolean().convert('anything else')
True