Ticket #11801: formset_deleted_forms.diff

File formset_deleted_forms.diff, 4.2 KB (added by dantallis, 15 years ago)
  • django/forms/formsets.py

     
    144144                # if this is an extra form and hasn't changed, don't consider it
    145145                if i >= self.initial_form_count() and not form.has_changed():
    146146                    continue
    147                 if form.cleaned_data[DELETION_FIELD_NAME]:
     147                if self._should_delete_form(form):
    148148                    self._deleted_form_indexes.append(i)
    149149        return [self.forms[i] for i in self._deleted_form_indexes]
    150150    deleted_forms = property(_get_deleted_forms)
     
    168168                if i >= self.initial_form_count() and not form.has_changed():
    169169                    continue
    170170                # don't add data marked for deletion to self.ordered_data
    171                 if self.can_delete and form.cleaned_data[DELETION_FIELD_NAME]:
     171                if self.can_delete and self._should_delete_form(form):
    172172                    continue
    173173                self._ordering.append((i, form.cleaned_data[ORDERING_FIELD_NAME]))
    174174            # After we're done populating self._ordering, sort it.
     
    212212        return self._errors
    213213    errors = property(_get_errors)
    214214
     215    def _should_delete_form(self, form):
     216        # The way we lookup the value of the deletion field here takes
     217        # more code than we'd like, but the form's cleaned_data will
     218        # not exist if the form is invalid.
     219        field = form.fields[DELETION_FIELD_NAME]
     220        raw_value = form._raw_value(DELETION_FIELD_NAME)
     221        should_delete = field.clean(raw_value)
     222        return should_delete
     223
    215224    def is_valid(self):
    216225        """
    217226        Returns True if form.errors is empty for every form in self.forms.
     
    224233        for i in range(0, self.total_form_count()):
    225234            form = self.forms[i]
    226235            if self.can_delete:
    227                 # The way we lookup the value of the deletion field here takes
    228                 # more code than we'd like, but the form's cleaned_data will
    229                 # not exist if the form is invalid.
    230                 field = form.fields[DELETION_FIELD_NAME]
    231                 raw_value = form._raw_value(DELETION_FIELD_NAME)
    232                 should_delete = field.clean(raw_value)
    233                 if should_delete:
     236                if self._should_delete_form(form):
    234237                    # This form is going to be deleted so any of its errors
    235238                    # should not cause the entire formset to be invalid.
    236239                    continue
  • tests/regressiontests/forms/formsets.py

     
    315315>>> formset.is_valid()
    316316False
    317317
     318Should be able to get deleted_forms from a valid formset even if a
     319deleted form would have been invalid.
     320
     321>>> class Person(Form):
     322...     name = CharField()
     323
     324>>> PeopleForm = formset_factory(
     325...     form=Person,
     326...     can_delete=True)
     327
     328>>> p = PeopleForm(
     329...     {'form-0-name': u'', 'form-0-DELETE': u'on', # no name!
     330...      'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1})
     331
     332>>> p.is_valid()
     333True
     334>>> len(p.deleted_forms)
     3351
     336
    318337# FormSets with ordering ######################################################
    319338
    320339We can also add ordering ability to a FormSet with an agrument to
     
    471490>>> [form.cleaned_data for form in formset.deleted_forms]
    472491[{'votes': 900, 'DELETE': True, 'ORDER': 2, 'choice': u'Fergie'}]
    473492
     493Should be able to get ordered forms from a valid formset even if a
     494deleted form would have been invalid.
    474495
     496>>> class Person(Form):
     497...     name = CharField()
     498
     499>>> PeopleForm = formset_factory(
     500...     form=Person,
     501...     can_delete=True,
     502...     can_order=True)
     503
     504>>> p = PeopleForm(
     505...     {'form-0-name': u'', 'form-0-DELETE': u'on', # no name!
     506...      'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1})
     507
     508>>> p.is_valid()
     509True
     510>>> p.ordered_forms
     511[]
     512
    475513# FormSet clean hook ##########################################################
    476514
    477515FormSets have a hook for doing extra validation that shouldn't be tied to any
Back to Top