Commit 4280217f authored by Ryan Kaskel's avatar Ryan Kaskel Committed by Carl Meyer
Browse files

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

parent 266c0bb2
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -303,7 +303,8 @@ class BaseFormSet(object):
            form = self.forms[i]
            self._errors.append(form.errors)
        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:
                raise ValidationError(ungettext(
                    "Please submit %d or fewer forms.",
+2 −1
Original line number Diff line number Diff line
@@ -283,7 +283,8 @@ Validating the number of forms in a formset

If ``validate_max=True`` is passed to
: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 myapp.forms import ArticleForm
+18 −0
Original line number Diff line number Diff line
@@ -986,6 +986,24 @@ class FormsFormsetTestCase(TestCase):
        self.assertEqual(list(formset.non_form_errors()),
            ['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 = {
    'choices-TOTAL_FORMS': '1', # the number of forms rendered