fields.py 737 Bytes
Newer Older
1 2 3
from django import forms
from django.core.validators import email_re

4

5 6 7 8 9 10 11 12 13 14 15 16 17 18
class MultipleEmailsField(forms.Field):
    def clean(self, value):
        """
        Check that the field contains one or more semicolon-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. Multiple email addresses Should be separated with semicolon (;)')
        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.
19
        return ';'.join(emails)