Code

Ticket #11907: 11907.2.diff

File 11907.2.diff, 1.4 KB (added by djansoft, 4 years ago)

Fix with test.

Line 
1diff --git a/django/forms/fields.py b/django/forms/fields.py
2index 2e6eb82..4793633 100644
3--- a/django/forms/fields.py
4+++ b/django/forms/fields.py
5@@ -414,6 +414,13 @@ class EmailField(CharField):
6         'invalid': _(u'Enter a valid e-mail address.'),
7     }
8     default_validators = [validators.validate_email]
9+       
10+    def clean(self, value):
11+        if value is None:
12+            value = u''
13+        value = smart_str(value).strip()
14+        value = super(EmailField, self).clean(value)
15+        return value
16 
17 class FileField(Field):
18     widget = FileInput
19diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
20index 8733090..bff6e70 100644
21--- a/tests/regressiontests/forms/fields.py
22+++ b/tests/regressiontests/forms/fields.py
23@@ -425,6 +425,7 @@ class FieldsTests(TestCase):
24         self.assertEqual(u'', f.clean(''))
25         self.assertEqual(u'', f.clean(None))
26         self.assertEqual(u'person@example.com', f.clean('person@example.com'))
27+        self.assertEqual(u'example@example.com', f.clean('      example@example.com  \t   \t '))
28         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo')
29         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@')
30         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid e-mail address.']", f.clean, 'foo@bar')