Login

MultipleEmailsField

Author:
spanasik
Posted:
April 24, 2009
Language:
Python
Version:
1.0
Score:
-1 (after 3 ratings)

This is a custom field for multiple emails separated by comma. Original code was replaced by code from Django documentation: http://docs.djangoproject.com/en/dev/ref/forms/validation/ (MultiEmailField) so i'm not the author of the code, but just put it here to replace an outdated solution.

Uses code from mksoft comment http://www.djangosnippets.org/snippets/1093/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import forms
from django.forms.fields import email_re

class MultipleEmailsField(forms.Field):
    def clean(self, value):
        """
        Check that the field contains one or more comma-separated emails
        and normalizes the data to a list of the email strings.
        """
        if not value:
            raise forms.ValidationError('Enter at least one e-mail address.')
        emails = value.split(',')
        for email in emails:
            if not email_re.match(email):
                raise forms.ValidationError('%s is not a valid e-mail address.' % email)

        # Always return the cleaned data.
        return emails

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

spanasik (on April 24, 2009):

Usage:

class SomeForm(forms.Form):
    emails = MultipleEmailsField()

#

diverman (on April 26, 2009):

Do you store multiple emails in one database column?

#

spanasik (on April 28, 2009):

No, it's required for user input, when multiple emails can be entered into one form field.

#

andybak (on May 28, 2009):

spanasik - the solution in the docs is identical but is missing the email_re here

#

asinox (on May 13, 2012):

Remember that email_re has moved from django.form.fields to django.core.validators

#

Please login first before commenting.