formalchemy.validators – Validation stuff¶
To validate data, you must bind it to your FieldSet
along with the SQLAlchemy model. Normally, you will retrieve data from a
dict:
>>> from formalchemy.tests import User, bill
>>> from formalchemy.forms import FieldSet
>>> fs = FieldSet(User)
>>> fs.configure(include=[fs.name]) # we only use the name field here
>>> fs.rebind(bill, data={'User-1-name': 'Sam'})
Validation is performed simply by invoking fs.validate(), which returns True if validation was successful, and False otherwise. Validation functions are added with the validate method, described above.
If validation fails, fs.errors will be populated. errors is a dictionary of validation failures, and is always empty before validate() is run. Dictionary keys are attributes; values are lists of messages given to ValidationException. Global errors (not specific to a single attribute) are under the key None.
Rendering a FieldSet with errors will result in
error messages being displayed inline. Here’s what this looks like for a
required field that was not supplied with a value:
<div>
<label class="field_req" for="foo">
Foo
</label>
<input id="foo" name="foo" type="text" value="" />
<span class="field_error">
Please enter a value
</span>
</div>
If validation succeeds, you can have FormAlchemy put the submitted data back into the bound model object with fs.sync. (If you bound to a class instead of an object, the class will be instantiated for you.) The object will be placed into the current session, if one exists:
>>> if fs.validate(): fs.sync()
>>> print(bill.name)
Sam
Exception¶
All validators raise a ValidationError if the validation failed.
-
exception
ValidationError¶
Validators¶
formalchemy.validators contains two types of functions: validation functions that can be used directly, and validation function _generators_ that _return_ a validation function satisfying some conditon. E.g., validators.maxlength(30) will return a validation function that can then be passed to validate.
>>> from formalchemy.validators import *
Validation Functions¶
A validation function is simply a function that, given a value, raises a ValidationError if it is invalid.
Function generators¶
Write your own validator¶
You can write your own validator, with the following function signature. The field parameter will be the Field object being validated (and though its .parent attribute, the FieldSet:
>>> def negative(value, field):
... if not (isinstance(value, int) and value < 0):
... raise ValidationError('Value must be less than 0')
Then bind it to a field:
>>> from formalchemy import types
>>> fs = FieldSet(One)
>>> fs.append(Field('number', type=types.Integer))
>>> fs.configure(include=[fs.number.validate(negative)])
Then it should work:
>>> fs.rebind(One, data={'One--number': '-2'})
>>> fs.validate()
True
>>> fs.rebind(One, data={'One--number': '2'})
>>> fs.validate()
False
You can also use the field positional argument to compare with some other fields in the same FieldSet if you know this will be contained in a FieldSet, for example:
>>> def passwd2_validator(value, field):
... if field.parent.passwd1.value != value:
... raise validators.ValidationError('Password do not match')
The FieldSet.errors and Field.errors attributes contain your custom error message:
>>> fs.errors
{Field(number): [literal('Value must be less than 0')]}
>>> fs.number.errors
[literal('Value must be less than 0')]