Fixed #20403 -- Ignore forms marked for deletion when validating max_num.

This commit is contained in:
Ryan Kaskel 2013-05-20 12:13:03 -04:00 committed by Carl Meyer
parent 266c0bb23e
commit 4280217f31
3 changed files with 22 additions and 2 deletions

View File

@ -303,7 +303,8 @@ class BaseFormSet(object):
form = self.forms[i] form = self.forms[i]
self._errors.append(form.errors) self._errors.append(form.errors)
try: try:
if (self.validate_max and self.total_form_count() > self.max_num) or \ if (self.validate_max and
self.total_form_count() - len(self.deleted_forms) > self.max_num) or \
self.management_form.cleaned_data[TOTAL_FORM_COUNT] > self.absolute_max: self.management_form.cleaned_data[TOTAL_FORM_COUNT] > self.absolute_max:
raise ValidationError(ungettext( raise ValidationError(ungettext(
"Please submit %d or fewer forms.", "Please submit %d or fewer forms.",

View File

@ -283,7 +283,8 @@ Validating the number of forms in a formset
If ``validate_max=True`` is passed to If ``validate_max=True`` is passed to
:func:`~django.forms.formsets.formset_factory`, validation will also check :func:`~django.forms.formsets.formset_factory`, validation will also check
that the number of forms in the data set is less than or equal to ``max_num``. that the number of forms in the data set, minus those marked for
deletion, is less than or equal to ``max_num``.
>>> from django.forms.formsets import formset_factory >>> from django.forms.formsets import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm

View File

@ -986,6 +986,24 @@ class FormsFormsetTestCase(TestCase):
self.assertEqual(list(formset.non_form_errors()), self.assertEqual(list(formset.non_form_errors()),
['This is a non-form error']) ['This is a non-form error'])
def test_validate_max_ignores_forms_marked_for_deletion(self):
class CheckForm(Form):
field = IntegerField()
data = {
'check-TOTAL_FORMS': '2',
'check-INITIAL_FORMS': '0',
'check-MAX_NUM_FORMS': '1',
'check-0-field': '200',
'check-0-DELETE': '',
'check-1-field': '50',
'check-1-DELETE': 'on',
}
CheckFormSet = formset_factory(CheckForm, max_num=1, validate_max=True,
can_delete=True)
formset = CheckFormSet(data, prefix='check')
self.assertTrue(formset.is_valid())
data = { data = {
'choices-TOTAL_FORMS': '1', # the number of forms rendered 'choices-TOTAL_FORMS': '1', # the number of forms rendered