Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#26844 closed Bug (fixed)

Formset's validate_min doesn't work correctly with empty forms

Reported by: Wim Feijen Owned by: Manasa Patibandla
Component: Forms Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

validate_min and min_num do not behave as I expected.

Defining a formset like this:

ImageFormSet = inlineformset_factory(
    Room,
    Image,
    form = ImageForm,
    extra = 5,
    min_num = 1,
    max_num = 5,
    validate_min = True,
)

...
        image_formset = ImageFormSet(request.POST, request.FILES, instance=room)

I filled in image_formset with this data. Note that I am posting one form with data and four empty forms.

(Pdb) image_formset.cleaned_data
[{'image': <InMemoryUploadedFile: gras.jpg (image/jpeg)>, 'room': <Room: Test wim yy>, u'id': None, u'DELETE': True}, {}, {}, {}, {}]

Due to the empty forms, this passes validation, contrary to my expectations.

This is because of:
lines 354 and 355 in django/forms/formsets.py

            if (self.validate_min and
                    self.total_form_count() - len(self.deleted_forms) < self.min_num):

and

(Pdb) image_formset.total_form_count()
5

total_form_count() turns out to be 5 instead of 1 .

Change History (8)

comment:1 Changed 3 years ago by Tim Graham

Component: UncategorizedForms
Summary: A formset with only one entry marked for deletion, passes validate_min = True and min_num = 1Formset's validate_min doesn't work correctly with deleted forms
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 Changed 3 years ago by Manasa Patibandla

Owner: changed from nobody to Manasa Patibandla
Status: newassigned

comment:3 Changed 3 years ago by Tim Graham

Has patch: set

comment:4 Changed 3 years ago by Tim Graham

Summary: Formset's validate_min doesn't work correctly with deleted formsFormset's validate_min doesn't work correctly with empty forms
Triage Stage: AcceptedReady for checkin

The issue here turns out to be empty forms be included in the form count.

comment:5 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In f5c6295:

Fixed #26844 -- Made formset's validate_min validation ignore empty forms.

comment:6 in reply to:  5 Changed 2 years ago by Jim Ouwerkerk

Resolution: fixed
Status: closednew

Replying to Tim Graham <timograham@…>:

In f5c6295:

Fixed #26844 -- Made formset's validate_min validation ignore empty forms.

This commit breaks the validate_min validation for formset with existing unchanged data in Django 1.11.
It counts unchanged existing forms as 'empty' form because they are not changed, but they should not be counted as empty.

comment:7 Changed 2 years ago by Simon Charette

Resolution: fixed
Status: newclosed

Jim, please file a new ticket instead of reopening this one. Just make sure to refer to this ticket to provide context.

comment:8 Changed 2 years ago by Tim Graham

The new ticket is #28130.

Note: See TracTickets for help on using tickets.
Back to Top