October 2018
Intermediate to advanced
332 pages
8h 9m
English
Writing a custom validation function is very simple. All that is required is to write a function that takes the form object and the field object as parameters and raises a WTForm. A ValidationError is raised if the data does not pass the test. Here is an example of a custom email validator:
import re
import wtforms
def custom_email(form, field):
if not re.match(r"[^@]+@[^@]+.[^@]+", field.data):
raise wtforms.ValidationError('Field must be a valid email address.')
To use this function, just add it to the list of validators for your field.