Skip to content

Commit

Permalink
Fixed #20403 -- Ignore forms marked for deletion when validating max_…
Browse files Browse the repository at this point in the history
…num.
  • Loading branch information
ryankask authored and carljm committed May 20, 2013
1 parent 266c0bb commit 4280217
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django/forms/formsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
3 changes: 2 additions & 1 deletion docs/topics/forms/formsets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/forms_tests/tests/test_formsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4280217

Please sign in to comment.