Code

Ticket #6362: 6362.diff

File 6362.diff, 3.2 KB (added by alekam, 3 years ago)

update to current trunk

Line 
1diff --git a/django/forms/fields.py b/django/forms/fields.py
2index 113a5aa..4093d15 100644
3--- a/django/forms/fields.py
4+++ b/django/forms/fields.py
5@@ -54,7 +54,7 @@ class Field(object):
6 
7     def __init__(self, required=True, widget=None, label=None, initial=None,
8                  help_text=None, error_messages=None, show_hidden_initial=False,
9-                 validators=[], localize=False):
10+                 validators=[], localize=False, normalize=None):
11         # required -- Boolean that specifies whether the field is required.
12         #             True by default.
13         # widget -- A Widget class, or instance of a Widget class, that should
14@@ -82,6 +82,7 @@ class Field(object):
15             self.help_text = u''
16         else:
17             self.help_text = smart_unicode(help_text)
18+        self.normalize = normalize
19         widget = widget or self.widget
20         if isinstance(widget, type):
21             widget = widget()
22@@ -149,6 +150,8 @@ class Field(object):
23         Raises ValidationError for any errors.
24         """
25         value = self.to_python(value)
26+        if self.normalize:
27+            value = self.normalize(value)
28         self.validate(value)
29         self.run_validators(value)
30         return value
31@@ -450,9 +453,10 @@ class EmailField(CharField):
32     }
33     default_validators = [validators.validate_email]
34 
35-    def clean(self, value):
36-        value = self.to_python(value).strip()
37-        return super(EmailField, self).clean(value)
38+    def __init__(self, *args, **kwargs):
39+        super(EmailField, self).__init__(*args, **kwargs)
40+        if self.normalize is None:
41+            self.normalize = lambda value: value.strip()
42 
43 class FileField(Field):
44     widget = ClearableFileInput
45diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
46index 66c05e5..e1333c3 100644
47--- a/docs/ref/forms/fields.txt
48+++ b/docs/ref/forms/fields.txt
49@@ -278,6 +278,23 @@ as the rendered output.
50 See the :ref:`format localization <format-localization>` documentation for
51 more information.
52 
53+``normalize``
54+~~~~~~~~~~~~
55+
56+.. versionadded:: New in Django development version
57+
58+.. attribute:: Field.normalize
59+
60+The ``normalize`` argument lets you normalize the input data before it is validated.
61+A common use-case is stripping leading and trailinig whitespace from CharFields::
62+
63+    >>> foo = forms.CharField(normalize=lambda x: x.strip())
64+    >>> foo.clean('   ')
65+    Traceback (most recent call last):
66+      ...
67+    ValidationError: [u'This field is required.']   
68+
69+
70 .. _built-in fields:
71 
72 Built-in ``Field`` classes
73diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
74index 6780413..37f18ce 100644
75--- a/tests/regressiontests/forms/tests/fields.py
76+++ b/tests/regressiontests/forms/tests/fields.py
77@@ -131,6 +131,10 @@ class FieldsTests(TestCase):
78         self.assertEqual(f.max_length, None)
79         self.assertEqual(f.min_length, 10)
80 
81+    def test_charfield_normalize(self):
82+        f = CharField(normalize=lambda x: x.strip())
83+        self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, "  \t")
84+
85     # IntegerField ################################################################
86 
87     def test_integerfield_1(self):