Saturday, November 3, 2012

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

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate