Add FormAlchemy-compatible validators for email address, phone number

This commit is contained in:
Lance Edgar 2016-09-18 17:16:17 -05:00
parent 93fcc72f73
commit 65661a03f5

View file

@ -21,14 +21,16 @@
#
################################################################################
"""
Custom FormEncode Validators
Custom Form Validators
"""
from __future__ import unicode_literals, absolute_import
from rattail.db import model
from rattail.db.util import validate_email_address, validate_phone_number
import formencode as fe
import formalchemy as fa
from tailbone.db import Session
@ -102,3 +104,27 @@ class ValidUser(ModelValidator):
Validator for user field.
"""
model_class = model.User
def valid_email_address(value, field=None):
"""
FormAlchemy-compatible validation function, which leverages FormEncode
under the hood.
"""
if value:
try:
return validate_email_address(value, error=True)
except Exception as error:
raise fa.ValidationError(unicode(error))
def valid_phone_number(value, field=None):
"""
FormAlchemy-compatible validation function, which leverages FormEncode
under the hood.
"""
if value:
try:
return validate_phone_number(value, error=True)
except Exception as error:
raise fa.ValidationError(unicode(error))