Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…num.
  • Loading branch information...
commit 4280217f31fc634d320b0cf30bcb6d582b19d784 1 parent 266c0bb
@ryankask ryankask authored carljm committed
View
3  django/forms/formsets.py
@@ -303,7 +303,8 @@ def full_clean(self):
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.",
View
3  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
View
18 tests/forms_tests/tests/test_formsets.py
@@ -986,6 +986,24 @@ def clean(self):
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

0 comments on commit 4280217

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