Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20403 -- Ignore forms marked for deletion when validating max_…

…num.
  • Loading branch information...
commit 4280217f31fc634d320b0cf30bcb6d582b19d784 1 parent 266c0bb
Ryan Kaskel authored carljm committed
3  django/forms/formsets.py
@@ -303,7 +303,8 @@ def full_clean(self):
303 303
             form = self.forms[i]
304 304
             self._errors.append(form.errors)
305 305
         try:
306  
-            if (self.validate_max and self.total_form_count() > self.max_num) or \
  306
+            if (self.validate_max and
  307
+                self.total_form_count() - len(self.deleted_forms) > self.max_num) or \
307 308
                 self.management_form.cleaned_data[TOTAL_FORM_COUNT] > self.absolute_max:
308 309
                 raise ValidationError(ungettext(
309 310
                     "Please submit %d or fewer forms.",
3  docs/topics/forms/formsets.txt
@@ -283,7 +283,8 @@ Validating the number of forms in a formset
283 283
 
284 284
 If ``validate_max=True`` is passed to
285 285
 :func:`~django.forms.formsets.formset_factory`, validation will also check
286  
-that the number of forms in the data set is less than or equal to ``max_num``.
  286
+that the number of forms in the data set, minus those marked for
  287
+deletion, is less than or equal to ``max_num``.
287 288
 
288 289
     >>> from django.forms.formsets import formset_factory
289 290
     >>> from myapp.forms import ArticleForm
18  tests/forms_tests/tests/test_formsets.py
@@ -986,6 +986,24 @@ def clean(self):
986 986
         self.assertEqual(list(formset.non_form_errors()),
987 987
             ['This is a non-form error'])
988 988
 
  989
+    def test_validate_max_ignores_forms_marked_for_deletion(self):
  990
+        class CheckForm(Form):
  991
+           field = IntegerField()
  992
+
  993
+        data = {
  994
+            'check-TOTAL_FORMS': '2',
  995
+            'check-INITIAL_FORMS': '0',
  996
+            'check-MAX_NUM_FORMS': '1',
  997
+            'check-0-field': '200',
  998
+            'check-0-DELETE': '',
  999
+            'check-1-field': '50',
  1000
+            'check-1-DELETE': 'on',
  1001
+        }
  1002
+        CheckFormSet = formset_factory(CheckForm, max_num=1, validate_max=True,
  1003
+                                       can_delete=True)
  1004
+        formset = CheckFormSet(data, prefix='check')
  1005
+        self.assertTrue(formset.is_valid())
  1006
+
989 1007
 
990 1008
 data = {
991 1009
     'choices-TOTAL_FORMS': '1', # the number of forms rendered

0 notes on commit 4280217

Please sign in to comment.
Something went wrong with that request. Please try again.