formular.validators - Validators

This module provides the built-in validators used by the fields and a helper to create your own validator factory ( as_validator() ).

The validators used by Formular are simple functions, which take the form, an initial value and the value which should be validated. If the validation is successful the function returns True otherwise False.

Error messages are handled by the field itself and are not created by the validator, as they may differ in the context of the field.

If you want to use your own validators on an exisitng field type checkout the formular.fields.Field.register_as_validator() documentation.

Built-in Validators

required(is_required)
Validates if the value is True, if is_required is True, otherwise it validates always.
requires(field_name)
Validates if the value of the field with the given field_name exists, if the value of the validated field exists.
equals(field_name)
Validates if the value of the field with the given field_name is equal to the value of the validated field.
min_length(min_length)
Validates if the length of the value is greater then the given min_length.
max_length(max_length)
Validates if the length of the value is lesser then the given max_length.
min_value(min_value)
Validates if the value is greater then the given max_value.
max_value(max_value)
Validates if the value is lesser then the given max_value.
is_email(email_required)

Validates if the value is a valid e-mail address.

Warning

This function does not implement rfc2822 completely.

New in version 0.2.

Creating Your Own Validator Factory

If you want to create your own field type, you will have to create a validator factory. Such a factory takes the initial value and returns a callable which takes the form and the value which should be validated. This way the field does not have to store the initial values.

The easiest way to create a validator factory, which can be put into formular.fields.Field.validator_factories, is to use as_validator().

as_validator(func)

Given a function like:

def required(form, is_required, value):
    return bool(value) if is_required else True

as_validator() returns a factory function:

>>> required_factory(required)
>>> required_factory(True)(form, u"")
False
>>> required_factory(False)(form, u"")

If you do not care about the original function you can use as_validator() to decorate the function.

Table Of Contents

Previous topic

formular.forms - Forms

Next topic

formular.widgets - (X)HTML Widgets

This Page