Ticket #5524: 5524-new.diff

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

Refresh the patch

  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index ad398c4..33ce291 100644
    a b class BaseForm(StrAndUnicode): 
    267267        self._clean_fields()
    268268        self._clean_form()
    269269        self._post_clean()
    270         if self._errors:
    271             del self.cleaned_data
    273271    def _clean_fields(self):
    274272        for name, field in self.fields.items():
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 254cca3..38b8b1b 100644
    a b class BaseModelFormSet(BaseFormSet): 
    496496        all_unique_checks = set()
    497497        all_date_checks = set()
    498498        for form in self.forms:
    499             if not hasattr(form, 'cleaned_data'):
     499            if not form.is_valid():
    500500                continue
    501501            exclude = form._get_validation_exclusions()
    502502            unique_checks, date_checks = form.instance._get_unique_checks(exclude=exclude)
    class BaseModelFormSet(BaseFormSet): 
    508508        for uclass, unique_check in all_unique_checks:
    509509            seen_data = set()
    510510            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():
    514512                    continue
    515513                # get data for each field of each of unique_check
    516514                row_data = tuple([form.cleaned_data[field] for field in unique_check if field in form.cleaned_data])
    class BaseModelFormSet(BaseFormSet): 
    530528            seen_data = set()
    531529            uclass, lookup, field, unique_for = date_check
    532530            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():
    536532                    continue
    537533                # see if we have data for both fields
    538534                if (form.cleaned_data and form.cleaned_data[field] is not None
  • docs/ref/forms/validation.txt

    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 
    360360considering aren't valid, we must remember to remove them from the
    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.
     363Before 1.4, Django used to completely wipe out the ``cleaned_data``
     364dictionary if there were any errors in the form.
     365From 1.4, the ``cleaned_data`` is present even if the form doesn't validate,
     366but it contains only field values that did validate (empty dictionary if none
  • tests/modeltests/model_forms/tests.py

    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): 
    637637        f = BaseCategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
    638638        self.assertEqual(f.errors['name'], [u'This field is required.'])
    639639        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'})
    642641        with self.assertRaises(ValueError):
    643642            f.save()
    644643        f = BaseCategoryForm({'name': '', 'slug': '', 'url': 'foo'})
  • tests/regressiontests/forms/tests/forms.py

    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): 
    7878        self.assertEqual(p.errors['last_name'], [u'This field is required.'])
    7979        self.assertEqual(p.errors['birthday'], [u'This field is required.'])
    8080        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, {})
    8682        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>
    8783<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>
    8884<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): 
    141137  * This field is required.
    142138* birthday
    143139  * 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'})
    149141        self.assertEqual(p['first_name'].errors, [u'This field is required.'])
    150142        self.assertEqual(p['first_name'].errors.as_ul(), u'<ul class="errorlist"><li>This field is required.</li></ul>')
    151143        self.assertEqual(p['first_name'].errors.as_text(), u'* This field is required.')
    class FormsTestCase(TestCase): 
    16361628        form = SongForm(data, empty_permitted=False)
    16371629        self.assertFalse(form.is_valid())
    16381630        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, {})
    16451633        # Now let's show what happens when empty_permitted=True and the form is empty.
    16461634        form = SongForm(data, empty_permitted=True)
    class FormsTestCase(TestCase): 
    16541642        form = SongForm(data, empty_permitted=False)
    16551643        self.assertFalse(form.is_valid())
    16561644        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'})
    16631647        # If a field is not given in the data then None is returned for its data. Lets
    16641648        # make sure that when checking for empty_permitted that None is treated
Back to Top