A validator simply takes an input, verifies it fulfills some criterion, such as a maximum length for a string and returns. Or, if the validation fails, raises a ValidationError. This system is very simple and flexible, and allows you to chain any number of validators on fields.
Causes the validation chain to stop.
If StopValidation is raised, no more validators in the validation chain are called. If raised with a message, the message will be added to the errors list.
Validates an email address. Note that this uses a very primitive regular expression and should only be used in instances where you later verify by other means, such as email activation or lookups.
Parameter: | message – Error message to raise in case of a validation error. |
---|
Compares the values of two fields.
Parameters: |
|
---|
This validator can be used to facilitate in one of the most common scenarios, the password change form:
class ChangePassword(Form):
password = PasswordField('New Password', [Required(), EqualTo('confirm', mesage='Passwords must match')])
confirm = PasswordField('Repeat Password')
In the example, we use the Required validator to prevent the EqualTo validator from trying to see if the passwords do not match if there was no passwords specified at all. Because Required stops the validation chain, EqualTo is not run in the case the password field is left empty.
Validates an IP(v4) address.
Parameter: | message – Error message to raise in case of a validation error. |
---|
Validates the length of a string.
Parameters: |
|
---|
Validates that the field contains data. This validator will stop the validation chain on error.
Parameter: | message – Error message to raise in case of a validation error. |
---|
Validates the field against a user provided regexp.
Parameters: |
|
---|
Simple regexp based url validation. Much like the email validator, you probably want to validate the url later by other means if the url must resolve.
Parameters: |
|
---|
Defining your own validators is easy. You simply make a function that takes a list of configuration directives, and then returns a callable. The returned callable should take two positional arguments, which are a form instance and the field instance being validated. It is helpful to design your validators with a message argument to provide a way to override the error message.
Let’s look at a possible validator which checks if a file upload’s extension is that of an image:
def is_image(message=u'Images only!', extensions=None):
if not extensions:
extensions = ('jpg', 'jpeg', 'png', 'gif')
def _is_image(form, field):
if not field.data or field.data.split('.')[-1] not in extensions:
raise ValidationError(message)
return _is_image
And the way it’s used:
avatar = FileField(u'Avatar', [is_image(u'Only images are allowed.', extensions=['gif', 'png'])])
The outer function sets configuration directives, in this case the message and the extensions. The inner function provides the actual validation: If the field contains no data, or an un-approved extension, ValidationError with the message is raised. Otherwise we just the let function return normally.
You could also define the validator as a class:
class IsImage(object):
def __init__(self, message=u'Images only!', extensions=None):
self.message = message
if not extensions:
extensions = ('jpg', 'jpeg', 'png', 'gif')
self.extensions = extensions
def __call__(self, form, field):
if not field.data or field.data.split('.')[-1] not in extensions:
raise ValidationError(self.message)
Which option you choose is entirely down to preference.