diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 3ec94d20ec..fd98c43405 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -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.", diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt index e55c22e3a2..68551d380b 100644 --- a/docs/topics/forms/formsets.txt +++ b/docs/topics/forms/formsets.txt @@ -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 diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index 31adb921ba..787f177677 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -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