Ticket #15349: unbound_empty_forms.diff

File unbound_empty_forms.diff, 2.8 KB (added by hidde-jan, 4 years ago)
  • django/forms/formsets.py

     
    144144            'prefix': self.add_prefix('__prefix__'),
    145145            'empty_permitted': True,
    146146        }
    147         if self.is_bound:
    148             defaults['data'] = self.data
    149             defaults['files'] = self.files
    150147        defaults.update(kwargs)
    151148        form = self.form(**defaults)
    152149        self.add_fields(form, None)
  • tests/regressiontests/forms/tests/formsets.py

     
    878878        self.assertFalse(formset.is_valid())
    879879        self.assertEquals([{}, {'pub_date': [u'This field is required.']}], formset.errors)
    880880
    881 class TestEmptyFormSet(TestCase):
     881    def test_empty_forms_are_unbound(self):
     882        data = {
     883            'form-TOTAL_FORMS': u'1',
     884            'form-INITIAL_FORMS': u'0',
     885            'form-0-title': u'Test',
     886            'form-0-pub_date': u'1904-06-16',
     887        }
     888        unbound_formset = ArticleFormSet()
     889        bound_formset = ArticleFormSet(data)
     890
     891        empty_forms = []
     892
     893        empty_forms.append(unbound_formset.empty_form)
     894        empty_forms.append(bound_formset.empty_form)
     895
     896        # Empty forms should be unbound
     897        self.assertFalse(empty_forms[0].is_bound)
     898        self.assertFalse(empty_forms[1].is_bound)
     899
     900        # The empty forms should be equal.
     901        self.assertEqual(empty_forms[0].as_p(), empty_forms[1].as_p())
     902
     903class TestEmptyFormSet(TestCase):
    882904    "Test that an empty formset still calls clean()"
    883     def test_empty_formset_is_valid(self): 
    884         EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate) 
    885         formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form") 
    886         formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form") 
    887         self.assertFalse(formset.is_valid()) 
    888         self.assertFalse(formset2.is_valid()) 
     905    def test_empty_formset_is_valid(self):
     906        EmptyFsetWontValidateFormset = formset_factory(FavoriteDrinkForm, extra=0, formset=EmptyFsetWontValidate)
     907        formset = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'0'},prefix="form")
     908        formset2 = EmptyFsetWontValidateFormset(data={'form-INITIAL_FORMS':'0', 'form-TOTAL_FORMS':'1', 'form-0-name':'bah' },prefix="form")
     909        self.assertFalse(formset.is_valid())
     910        self.assertFalse(formset2.is_valid())
    889911
Back to Top