Ticket #11418: formset.diff

File formset.diff, 2.2 KB (added by Mark Lavin, 15 years ago)

Change to BaseFormSet._construct_form with tests

  • django/forms/formsets.py

     
    9292        Instantiates and returns the i-th form instance in a formset.
    9393        """
    9494        defaults = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)}
    95         if self.data or self.files:
     95        if self.is_bound:
    9696            defaults['data'] = self.data
    9797            defaults['files'] = self.files
    9898        if self.initial:
  • tests/regressiontests/forms/formsets.py

     
    55FormSet allows us to use multiple instance of the same form on 1 page. For now,
    66the best way to create a FormSet is by using the formset_factory function.
    77
    8 >>> from django.forms import Form, CharField, IntegerField, ValidationError
     8>>> from django.forms import Form, CharField, IntegerField, ValidationError, DateField
    99>>> from django.forms.formsets import formset_factory, BaseFormSet
    1010
    1111>>> class Choice(Form):
     
    598598>>> formset.management_form.prefix
    599599'form'
    600600
     601
     602# Regression test for #11418 #################################################
     603
     604>>> class ArticleForm(Form):
     605...     title = CharField()
     606...     pub_date = DateField()
     607
     608>>> ArticleFormSet = formset_factory(ArticleForm)
     609>>> formset = ArticleFormSet({})
     610>>> formset.initial_form_count()
     6110
     612>>> formset.total_form_count()
     6131
     614>>> formset.is_bound
     615True
     616>>> formset.forms[0].is_bound
     617True
     618>>> formset.is_valid()
     619True
     620>>> formset.forms[0].is_valid()
     621True
     622>>> formset.cleaned_data
     623[{}]
     624>>> data = {
     625...     'form-TOTAL_FORMS': u'2',
     626...     'form-INITIAL_FORMS': u'0',
     627...     'form-0-title': u'Test',
     628...     'form-0-pub_date': u'16 June 1904',
     629...     'form-1-title': u'Test',
     630...     'form-1-pub_date': u'', # <-- this date is missing but required
     631... }
     632>>> formset = ArticleFormSet(data)
     633>>> formset.is_valid()
     634False
     635>>> formset.errors
     636[{}, {'pub_date': [u'This field is required.']}]
     637
    601638"""
Back to Top