Ticket #18574: 18574_2.diff

File 18574_2.diff, 2.3 KB (added by foonicorn, 12 years ago)

Tests for charettes' patch

  • django/forms/formsets.py

    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index a0a38f3..3893cc5 100644
    a b class BaseFormSet(object):  
    267267
    268268    def is_valid(self):
    269269        """
    270         Returns True if form.errors is empty for every form in self.forms.
     270        Returns True if every form in self.forms is valid.
    271271        """
    272272        if not self.is_bound:
    273273            return False
    class BaseFormSet(object):  
    282282                    # This form is going to be deleted so any of its errors
    283283                    # should not cause the entire formset to be invalid.
    284284                    continue
    285             if bool(self.errors[i]):
    286                 forms_valid = False
     285            forms_valid &= form.is_valid()
    287286        return forms_valid and not bool(self.non_form_errors())
    288287
    289288    def full_clean(self):
  • tests/regressiontests/forms/tests/formsets.py

    diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py
    index b3ceee5..bf893c4 100644
    a b class FormsFormsetTestCase(TestCase):  
    856856        formset = FavoriteDrinksFormSet(error_class=CustomErrorList)
    857857        self.assertEqual(formset.forms[0].error_class, CustomErrorList)
    858858
     859    def test_formset_calls_forms_is_valid(self):
     860        # Regression tests for #18574 -- make sure formsets call
     861        # is_valid() on each form.
     862
     863        class AnotherChoice(Choice):
     864            def is_valid(self):
     865                self.is_valid_called = True
     866                return super(AnotherChoice, self).is_valid()
     867
     868        AnotherChoiceFormSet = formset_factory(AnotherChoice)
     869        data = {
     870            'choices-TOTAL_FORMS': '1',  # number of forms rendered
     871            'choices-INITIAL_FORMS': '0',  # number of forms with initial data
     872            'choices-MAX_NUM_FORMS': '0',  # max number of forms
     873            'choices-0-choice': 'Calexico',
     874            'choices-0-votes': '100',
     875        }
     876        formset = AnotherChoiceFormSet(data, auto_id=False, prefix='choices')
     877        self.assertTrue(formset.is_valid())
     878        self.assertTrue(all([form.is_valid_called for form in formset.forms]))
     879
    859880
    860881data = {
    861882    'choices-TOTAL_FORMS': '1', # the number of forms rendered
Back to Top