This module provides the built-in fields.
Fields representing basic types like strings, numbers etc.
Bases: formular.fields.SizedField
Represents a field which holds text.
Parameters: |
---|
Changed in version 0.2: Automatic change to Textarea if the default value contains a newline.
Bases: formular.fields.TextField
Represents a field which holds a password.
Bases: formular.fields.ComparableField
Represents a field which holds a number of a given type.
Parameter: | number_type – The constructor of the number type this field should hold. If the value is None the value number_type attribute of the class is used instead. |
---|
Bases: formular.fields.Field
Represents a field which holds a boolean value.
Represents a submit button.
New in version 0.2.
Bases: formular.fields.Field
Represents a field which allows the user to choose between different values.
Parameter: | choices – A list of values or a list of pairs. If a list of pairs is passed each pair should be a tuple of a value and a label which should be used instead of the value to be displayed to the user. The choices can always be changed later using the choices attribute. |
---|
Bases: formular.fields.ChoiceField, formular.fields.SizedField
Represents a field which allows the user to make multiple choices between different values.
Represents a reCAPTCHA.
Parameters: |
|
---|
Warning
Only one ReCAPTCHAField can be bound to a form.
New in version 0.2.
Note
These fields require Babel.
Bases: formular.fields.ComparableField
Represents a datetime.time instance.
Parameter: | format – One of "full", "long" or "medium". |
---|
Bases: formular.fields.ComparableField
Represents a datetime.date instance.
Parameter: | format – “short”` or a custom date/time pattern. |
---|
Fields representing collections/datastructures.
Bases: formular.fields.SizedField
Represents multiple values of the same field.
>>> field = Multiple(IntegerField())
>>> field.validate([u"1", u"2", u"3"])
True
>>> field.value
[1, 2, 3]
Bases: formular.fields.Multiple
Represents multiple values seperated by a string.
Parameters: |
|
---|
Bases: formular.fields.Field
Represents multiple different fields:
>>> field = Mapping({"name": TextField(), "age": IntegerField()})
>>> field.validate({"name": u"John Doe", "age": u"42"})
Parameter: | mapped_fields – A dictionary or a list of pairs, with names as keys and fields as their values. |
---|
Making your own field type is easy. You have to write a method which represents the value, the field represents as a string (to_primitive()) and a method which converts this string back to the value (convert()).
If the unicode representation is enough to convert it you don’t even have to implement to_primitive().
In addition to that you can define a default value for the type, if formular.datastructure.missing doesn’t fit, add the validators (validator_factories) and the error messages (error_messages).
However if there is already an abstract field implementing the behavior of the type you might not even have to add the validators and the error messages for those validators.
Fields implementing the validators and error messages for values with the specified behavior.
Bases: formular.fields.Field
Represents a field, which value has a length.
Bases: formular.fields.Field
Represents a field with a comparable value.
The base class of any field.
Parameters: |
|
---|
Validators are passed as keyword arguments. The name of the validator is passed as key with the initial value as value.
Validators and error messages are inherited by subclasses.
Returns a widget_type instance for this field. If no widget_type is given, widget is used instead.
Parameter: | dialect – The dialect used to render this widget. Supported are "html" and "xhtml". |
---|
Converts the given value to unicode.
This method gets a unicode representation of the value this method should return. The given value should be a result of to_primitive() but it can be anything.
Therefore, if a conversion is not possible and the value is invalid, a ValueError may be raised. If that happens the “invalid” error message is added to errors.
Returns a copy of the field, arguments passed to the constructor can be overridden by passing them.
Note
Copies are always unbound, even if the copied field is bound.
Decorator to add a validator.
Parameters: |
|
---|
This method can be used like this:
@Field.register_as_validator
def required(form, is_required, value):
return bool(value) if is_required else True
Or if you want to pass the message directly:
@Field.register_as_validator(message=u"This field is required")
...
However you might not want to have a required - or whatever the name is - function lying around somewhere, so you can just define the name explicitly:
@Field.register_as_validator("required")
def a_name_i_like_a_lot_more(form, is_required, value):
return bool(value) if is_required else True
Of course you can also pass the message parameter here.
Note
If you register a validator on a field class, this validator will be registered on every subclass of this field. The same goes for error messages
Converts the given value to unicode.
A subclass may want to override this method if the unicode representation of value is not sufficient enough to get the value back.
However this method has to return a unicode string and must not fail.
Converts and validates value. If the validation fails with one or more errors this method returns False otherwise True.
Parameters: |
|
---|
The parsed value can be accessed through the value attribute, the errors are stored as a formular.exceptions.ValidationError through the errors attribute.