Added validation method and exception for email schema field
authorThierry Florac <thierry.florac@onf.fr>
Fri, 13 Oct 2017 09:55:57 +0200
changeset 114 859dfc9be3ff
parent 113 5a80a143d2ec
child 115 0fc08a1bc6e7
Added validation method and exception for email schema field
src/pyams_utils/schema.py
--- a/src/pyams_utils/schema.py	Thu Sep 21 12:43:22 2017 +0200
+++ b/src/pyams_utils/schema.py	Fri Oct 13 09:55:57 2017 +0200
@@ -177,10 +177,15 @@
 EMAIL_REGEX = re.compile("^[^ @]+@[^ @]+\.[^ @]+$")
 
 
+class InvalidEmail(ValidationError):
+    __doc__ = _("Email address must be entered as « name@domain.name », without '<' and '>' characters")
+
+
 @implementer(IMailAddressField)
 class MailAddressField(TextLine):
     """Mail address field"""
 
-    def __init__(self, *args, **kwargs):
-        kwargs['constraint'] = EMAIL_REGEX.match
-        super(MailAddressField, self).__init__(*args, **kwargs)
+    def _validate(self, value):
+        super(MailAddressField, self)._validate(value)
+        if not EMAIL_REGEX.match(value):
+            raise InvalidEmail(value)