RE: Help with Custom model field and custom validation for EmailField()

6 views
Skip to first unread message

bb...@yahoo.com

unread,
Nov 4, 2012, 1:16:26 AM11/4/12
to django...@googlegroups.com
Hi,
If all u nid is a field whose value is a comma separated list of emails den u dont nid a custom field at all. All u nid is a gud ol CharField. Just set it like so:
emails=models.CharField(max_length=500, validators=[multi_email_validator,]).
here is what multi_email_validator looks like:
from django.core.validators import EmailValidator
from django.core.exceptions import ValidationError
def multi_email_validator(value):
if value and "," in value:
for v in value.split(",").strip():
try:
EmailValidator().__call__(v)
except: raise ValidationError("%s is not a valid email" % v)
else:
try:
EmailValidator().__call__(value)
except: raise ValidationError("%s is not a valid email" % value)
with dis aproach and using modelforms u dont nid to validate in forms or views cos calling form.is_valid with call model field validators too. Hope dis helps.
abraham.

--------------------------
Sent from my mobile device
Reply all
Reply to author
Forward
0 new messages