From bf0947a4743ce7df748f6e78a3d6e7d9e4f6cf5d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 7 Oct 2010 15:50:36 +0000 Subject: [PATCH] Fixed #11907 -- EmailField now runs strip() on its input. This means mistakenly including leading or trailing spaces will not cause a validation error, and clean() will remove those spaces. Thanks, krisneuharth, djansoft and SmileyChris git-svn-id: http://code.djangoproject.com/svn/django/trunk@13997 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/fields.py | 4 ++++ tests/regressiontests/forms/fields.py | 1 + 2 files changed, 5 insertions(+) diff --git a/django/forms/fields.py b/django/forms/fields.py index 03455e0989..51551ded47 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -448,6 +448,10 @@ class EmailField(CharField): } default_validators = [validators.validate_email] + def clean(self, value): + value = self.to_python(value).strip() + return super(EmailField, self).clean(value) + class FileField(Field): widget = ClearableFileInput default_error_messages = { diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index d29e2e8e43..cf0c3f659c 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -430,6 +430,7 @@ class FieldsTests(TestCase): self.assertEqual(u'', f.clean('')) self.assertEqual(u'', f.clean(None)) self.assertEqual(u'person@example.com', f.clean('person@example.com')) + self.assertEqual(u'example@example.com', f.clean(' example@example.com \t \t ')) self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo') self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@') self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')