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 .
--
Ticket URL: <https://code.djangoproject.com/ticket/26844>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Uncategorized => Forms
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:1>
* owner: nobody => mpatibandla
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/6930 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:3>
* stage: Accepted => Ready for checkin
Comment:
The issue here turns out to be empty forms be included in the form count.
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f5c6295797b8332134fd89e0209a18a1d1d45e0c" f5c6295]:
{{{
#!CommitTicketReference repository=""
revision="f5c6295797b8332134fd89e0209a18a1d1d45e0c"
Fixed #26844 -- Made formset's validate_min validation ignore empty forms.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:5>
* status: closed => new
* resolution: fixed =>
Comment:
Replying to [comment:5 Tim Graham <timograham@…>]:
> In [changeset:"f5c6295797b8332134fd89e0209a18a1d1d45e0c" f5c6295]:
> {{{
> #!CommitTicketReference repository=""
revision="f5c6295797b8332134fd89e0209a18a1d1d45e0c"
> 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:6>
* status: new => closed
* resolution: => fixed
Comment:
Jim, please file a new ticket instead of reopening this one. Just make
sure to refer to this ticket to provide context.
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:7>
Comment (by Tim Graham):
The new ticket is #28130.
--
Ticket URL: <https://code.djangoproject.com/ticket/26844#comment:8>