#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 , 9 years ago
| Component: | Uncategorized → Forms |
|---|---|
| Summary: | A formset with only one entry marked for deletion, passes validate_min = True and min_num = 1 → Formset's validate_min doesn't work correctly with deleted forms |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:2 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:3 by , 9 years ago
| Has patch: | set |
|---|
comment:4 by , 9 years ago
| Summary: | Formset's validate_min doesn't work correctly with deleted forms → Formset's validate_min doesn't work correctly with empty forms |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
The issue here turns out to be empty forms be included in the form count.
comment:6 by , 9 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → new |
Replying to Tim Graham <timograham@…>:
In f5c6295:
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 , 9 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Jim, please file a new ticket instead of reopening this one. Just make sure to refer to this ticket to provide context.
PR