Code

Ticket #5524: 5524-new.diff

File 5524-new.diff, 7.0 KB (added by claudep, 3 years ago)

Refresh the patch

Line 
1diff --git a/django/forms/forms.py b/django/forms/forms.py
2index ad398c4..33ce291 100644
3--- a/django/forms/forms.py
4+++ b/django/forms/forms.py
5@@ -267,8 +267,6 @@ class BaseForm(StrAndUnicode):
6         self._clean_fields()
7         self._clean_form()
8         self._post_clean()
9-        if self._errors:
10-            del self.cleaned_data
11 
12     def _clean_fields(self):
13         for name, field in self.fields.items():
14diff --git a/django/forms/models.py b/django/forms/models.py
15index 254cca3..38b8b1b 100644
16--- a/django/forms/models.py
17+++ b/django/forms/models.py
18@@ -496,7 +496,7 @@ class BaseModelFormSet(BaseFormSet):
19         all_unique_checks = set()
20         all_date_checks = set()
21         for form in self.forms:
22-            if not hasattr(form, 'cleaned_data'):
23+            if not form.is_valid():
24                 continue
25             exclude = form._get_validation_exclusions()
26             unique_checks, date_checks = form.instance._get_unique_checks(exclude=exclude)
27@@ -508,9 +508,7 @@ class BaseModelFormSet(BaseFormSet):
28         for uclass, unique_check in all_unique_checks:
29             seen_data = set()
30             for form in self.forms:
31-                # if the form doesn't have cleaned_data then we ignore it,
32-                # it's already invalid
33-                if not hasattr(form, "cleaned_data"):
34+                if not form.is_valid():
35                     continue
36                 # get data for each field of each of unique_check
37                 row_data = tuple([form.cleaned_data[field] for field in unique_check if field in form.cleaned_data])
38@@ -530,9 +528,7 @@ class BaseModelFormSet(BaseFormSet):
39             seen_data = set()
40             uclass, lookup, field, unique_for = date_check
41             for form in self.forms:
42-                # if the form doesn't have cleaned_data then we ignore it,
43-                # it's already invalid
44-                if not hasattr(self, 'cleaned_data'):
45+                if not form.is_valid():
46                     continue
47                 # see if we have data for both fields
48                 if (form.cleaned_data and form.cleaned_data[field] is not None
49diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt
50index 5264279..e5a741c 100644
51--- a/docs/ref/forms/validation.txt
52+++ b/docs/ref/forms/validation.txt
53@@ -360,7 +360,8 @@ Secondly, once we have decided that the combined data in the two fields we are
54 considering aren't valid, we must remember to remove them from the
55 ``cleaned_data``.
56 
57-In fact, Django will currently completely wipe out the ``cleaned_data``
58-dictionary if there are any errors in the form. However, this behavior may
59-change in the future, so it's not a bad idea to clean up after yourself in the
60-first place.
61+Before 1.4, Django used to completely wipe out the ``cleaned_data``
62+dictionary if there were any errors in the form.
63+From 1.4, the ``cleaned_data`` is present even if the form doesn't validate,
64+but it contains only field values that did validate (empty dictionary if none
65+validated).
66diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
67index 09a9d3f..45e15d3 100644
68--- a/tests/modeltests/model_forms/tests.py
69+++ b/tests/modeltests/model_forms/tests.py
70@@ -637,8 +637,7 @@ class OldFormForXTests(TestCase):
71         f = BaseCategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
72         self.assertEqual(f.errors['name'], [u'This field is required.'])
73         self.assertEqual(f.errors['slug'], [u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."])
74-        with self.assertRaises(AttributeError):
75-            f.cleaned_data
76+        self.assertEqual(f.cleaned_data, {'url': u'foo'})
77         with self.assertRaises(ValueError):
78             f.save()
79         f = BaseCategoryForm({'name': '', 'slug': '', 'url': 'foo'})
80diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
81index ed783af..6bbc54e 100644
82--- a/tests/regressiontests/forms/tests/forms.py
83+++ b/tests/regressiontests/forms/tests/forms.py
84@@ -78,11 +78,7 @@ class FormsTestCase(TestCase):
85         self.assertEqual(p.errors['last_name'], [u'This field is required.'])
86         self.assertEqual(p.errors['birthday'], [u'This field is required.'])
87         self.assertFalse(p.is_valid())
88-        try:
89-            p.cleaned_data
90-            self.fail('Attempts to access cleaned_data when validation fails should fail.')
91-        except AttributeError:
92-            pass
93+        self.assertEqual(p.cleaned_data, {})
94         self.assertEqual(str(p), """<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr>
95 <tr><th><label for="id_last_name">Last name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="last_name" id="id_last_name" /></td></tr>
96 <tr><th><label for="id_birthday">Birthday:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="birthday" id="id_birthday" /></td></tr>""")
97@@ -141,11 +137,7 @@ class FormsTestCase(TestCase):
98   * This field is required.
99 * birthday
100   * This field is required.""")
101-        try:
102-            p.cleaned_data
103-            self.fail('Attempts to access cleaned_data when validation fails should fail.')
104-        except AttributeError:
105-            pass
106+        self.assertEqual(p.cleaned_data, {'last_name': u'Lennon'})
107         self.assertEqual(p['first_name'].errors, [u'This field is required.'])
108         self.assertEqual(p['first_name'].errors.as_ul(), u'<ul class="errorlist"><li>This field is required.</li></ul>')
109         self.assertEqual(p['first_name'].errors.as_text(), u'* This field is required.')
110@@ -1636,11 +1628,7 @@ class FormsTestCase(TestCase):
111         form = SongForm(data, empty_permitted=False)
112         self.assertFalse(form.is_valid())
113         self.assertEqual(form.errors, {'name': [u'This field is required.'], 'artist': [u'This field is required.']})
114-        try:
115-            form.cleaned_data
116-            self.fail('Attempts to access cleaned_data when validation fails should fail.')
117-        except AttributeError:
118-            pass
119+        self.assertEqual(form.cleaned_data, {})
120 
121         # Now let's show what happens when empty_permitted=True and the form is empty.
122         form = SongForm(data, empty_permitted=True)
123@@ -1654,11 +1642,7 @@ class FormsTestCase(TestCase):
124         form = SongForm(data, empty_permitted=False)
125         self.assertFalse(form.is_valid())
126         self.assertEqual(form.errors, {'name': [u'This field is required.']})
127-        try:
128-            form.cleaned_data
129-            self.fail('Attempts to access cleaned_data when validation fails should fail.')
130-        except AttributeError:
131-            pass
132+        self.assertEqual(form.cleaned_data, {'artist': u'The Doors'})
133 
134         # If a field is not given in the data then None is returned for its data. Lets
135         # make sure that when checking for empty_permitted that None is treated