validators.py 626 B

123456789101112131415161718192021
  1. import re
  2. import six
  3. from wtforms import validators
  4. class EmailValidator(object):
  5. """Validates wtform email field"""
  6. def __init__(self, message="The field data was not an email"):
  7. self._message = message
  8. def __call__(self, form, email):
  9. if not isinstance(email.data, six.text_type):
  10. raise validators.ValidationError(
  11. "Email fields should be of text type. Found {0}".format(
  12. type(email.data)
  13. )
  14. )
  15. elif not re.match(r"[^@]+@[^@]+\.[^@]+", email.data):
  16. raise validators.ValidationError(self._message)