diff --git a/django/forms/forms.py b/django/forms/forms.py
index ad398c4..33ce291 100644
|
a
|
b
|
class BaseForm(StrAndUnicode):
|
| 267 | 267 | self._clean_fields() |
| 268 | 268 | self._clean_form() |
| 269 | 269 | self._post_clean() |
| 270 | | if self._errors: |
| 271 | | del self.cleaned_data |
| 272 | 270 | |
| 273 | 271 | def _clean_fields(self): |
| 274 | 272 | for name, field in self.fields.items(): |
diff --git a/django/forms/models.py b/django/forms/models.py
index 254cca3..38b8b1b 100644
|
a
|
b
|
class BaseModelFormSet(BaseFormSet):
|
| 496 | 496 | all_unique_checks = set() |
| 497 | 497 | all_date_checks = set() |
| 498 | 498 | for form in self.forms: |
| 499 | | if not hasattr(form, 'cleaned_data'): |
| | 499 | if not form.is_valid(): |
| 500 | 500 | continue |
| 501 | 501 | exclude = form._get_validation_exclusions() |
| 502 | 502 | unique_checks, date_checks = form.instance._get_unique_checks(exclude=exclude) |
| … |
… |
class BaseModelFormSet(BaseFormSet):
|
| 508 | 508 | for uclass, unique_check in all_unique_checks: |
| 509 | 509 | seen_data = set() |
| 510 | 510 | for form in self.forms: |
| 511 | | # if the form doesn't have cleaned_data then we ignore it, |
| 512 | | # it's already invalid |
| 513 | | if not hasattr(form, "cleaned_data"): |
| | 511 | if not form.is_valid(): |
| 514 | 512 | continue |
| 515 | 513 | # get data for each field of each of unique_check |
| 516 | 514 | row_data = tuple([form.cleaned_data[field] for field in unique_check if field in form.cleaned_data]) |
| … |
… |
class BaseModelFormSet(BaseFormSet):
|
| 530 | 528 | seen_data = set() |
| 531 | 529 | uclass, lookup, field, unique_for = date_check |
| 532 | 530 | for form in self.forms: |
| 533 | | # if the form doesn't have cleaned_data then we ignore it, |
| 534 | | # it's already invalid |
| 535 | | if not hasattr(self, 'cleaned_data'): |
| | 531 | if not form.is_valid(): |
| 536 | 532 | continue |
| 537 | 533 | # see if we have data for both fields |
| 538 | 534 | if (form.cleaned_data and form.cleaned_data[field] is not None |
diff --git a/docs/ref/forms/validation.txt b/docs/ref/forms/validation.txt
index 5264279..e5a741c 100644
|
a
|
b
|
Secondly, once we have decided that the combined data in the two fields we are
|
| 360 | 360 | considering aren't valid, we must remember to remove them from the |
| 361 | 361 | ``cleaned_data``. |
| 362 | 362 | |
| 363 | | In fact, Django will currently completely wipe out the ``cleaned_data`` |
| 364 | | dictionary if there are any errors in the form. However, this behavior may |
| 365 | | change in the future, so it's not a bad idea to clean up after yourself in the |
| 366 | | first place. |
| | 363 | Before 1.4, Django used to completely wipe out the ``cleaned_data`` |
| | 364 | dictionary if there were any errors in the form. |
| | 365 | From 1.4, the ``cleaned_data`` is present even if the form doesn't validate, |
| | 366 | but it contains only field values that did validate (empty dictionary if none |
| | 367 | validated). |
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index 09a9d3f..45e15d3 100644
|
a
|
b
|
class OldFormForXTests(TestCase):
|
| 637 | 637 | f = BaseCategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'}) |
| 638 | 638 | self.assertEqual(f.errors['name'], [u'This field is required.']) |
| 639 | 639 | self.assertEqual(f.errors['slug'], [u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]) |
| 640 | | with self.assertRaises(AttributeError): |
| 641 | | f.cleaned_data |
| | 640 | self.assertEqual(f.cleaned_data, {'url': u'foo'}) |
| 642 | 641 | with self.assertRaises(ValueError): |
| 643 | 642 | f.save() |
| 644 | 643 | f = BaseCategoryForm({'name': '', 'slug': '', 'url': 'foo'}) |
diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
index ed783af..6bbc54e 100644
|
a
|
b
|
class FormsTestCase(TestCase):
|
| 78 | 78 | self.assertEqual(p.errors['last_name'], [u'This field is required.']) |
| 79 | 79 | self.assertEqual(p.errors['birthday'], [u'This field is required.']) |
| 80 | 80 | self.assertFalse(p.is_valid()) |
| 81 | | try: |
| 82 | | p.cleaned_data |
| 83 | | self.fail('Attempts to access cleaned_data when validation fails should fail.') |
| 84 | | except AttributeError: |
| 85 | | pass |
| | 81 | self.assertEqual(p.cleaned_data, {}) |
| 86 | 82 | 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> |
| 87 | 83 | <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> |
| 88 | 84 | <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>""") |
| … |
… |
class FormsTestCase(TestCase):
|
| 141 | 137 | * This field is required. |
| 142 | 138 | * birthday |
| 143 | 139 | * This field is required.""") |
| 144 | | try: |
| 145 | | p.cleaned_data |
| 146 | | self.fail('Attempts to access cleaned_data when validation fails should fail.') |
| 147 | | except AttributeError: |
| 148 | | pass |
| | 140 | self.assertEqual(p.cleaned_data, {'last_name': u'Lennon'}) |
| 149 | 141 | self.assertEqual(p['first_name'].errors, [u'This field is required.']) |
| 150 | 142 | self.assertEqual(p['first_name'].errors.as_ul(), u'<ul class="errorlist"><li>This field is required.</li></ul>') |
| 151 | 143 | self.assertEqual(p['first_name'].errors.as_text(), u'* This field is required.') |
| … |
… |
class FormsTestCase(TestCase):
|
| 1636 | 1628 | form = SongForm(data, empty_permitted=False) |
| 1637 | 1629 | self.assertFalse(form.is_valid()) |
| 1638 | 1630 | self.assertEqual(form.errors, {'name': [u'This field is required.'], 'artist': [u'This field is required.']}) |
| 1639 | | try: |
| 1640 | | form.cleaned_data |
| 1641 | | self.fail('Attempts to access cleaned_data when validation fails should fail.') |
| 1642 | | except AttributeError: |
| 1643 | | pass |
| | 1631 | self.assertEqual(form.cleaned_data, {}) |
| 1644 | 1632 | |
| 1645 | 1633 | # Now let's show what happens when empty_permitted=True and the form is empty. |
| 1646 | 1634 | form = SongForm(data, empty_permitted=True) |
| … |
… |
class FormsTestCase(TestCase):
|
| 1654 | 1642 | form = SongForm(data, empty_permitted=False) |
| 1655 | 1643 | self.assertFalse(form.is_valid()) |
| 1656 | 1644 | self.assertEqual(form.errors, {'name': [u'This field is required.']}) |
| 1657 | | try: |
| 1658 | | form.cleaned_data |
| 1659 | | self.fail('Attempts to access cleaned_data when validation fails should fail.') |
| 1660 | | except AttributeError: |
| 1661 | | pass |
| | 1645 | self.assertEqual(form.cleaned_data, {'artist': u'The Doors'}) |
| 1662 | 1646 | |
| 1663 | 1647 | # If a field is not given in the data then None is returned for its data. Lets |
| 1664 | 1648 | # make sure that when checking for empty_permitted that None is treated |