Opened 8 years ago

Closed 7 years ago

Last modified 7 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 by Tim Graham, 8 years ago

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 by Manasa Patibandla, 8 years ago

Owner: changed from nobody to Manasa Patibandla
Status: newassigned

comment:3 by Tim Graham, 8 years ago

Has patch: set

comment:4 by Tim Graham, 8 years ago

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 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In f5c6295:

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

in reply to:  5 comment:6 by Jim Ouwerkerk, 7 years ago

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 by Simon Charette, 7 years ago

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 by Tim Graham, 7 years ago

The new ticket is #28130.

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