Opened 5 years ago

Closed 5 years ago

#30940 closed Bug (invalid)

Documentation on formsets lists incomplete code to detect errors in clean()

Reported by: Tiemo Owned by: nobody
Component: Documentation Version: 2.2
Severity: Normal Keywords: documentation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation on FormSets at https://docs.djangoproject.com/en/2.2/topics/forms/formsets/ lists incomplete sample code to determine whether the formset has erorrs in clean().

The listing:

>>> class BaseArticleFormSet(BaseFormSet):
...     def clean(self):
...         """Checks that no two articles have the same title."""
...         if any(self.errors):
...             # Don't bother validating the formset unless each form is valid on its own
...             return

This check is not complete, the full_clean method calls form.errors on all forms in the formset at r349, even on forms that are to be removed. This results in self.errors containing errors for forms that are to be deleted.

However, FormSet.is_valid ignores errors in forms that are to be removed.

The result is that the any(self.errors) snippet returns True, even if only removed forms contain errors.

Change History (1)

comment:1 by Carlton Gibson, 5 years ago

Easy pickings: unset
Resolution: invalid
Status: newclosed

Since f32d24652b920135eb6a0f3de74599f03e181731 forms that are to be deleted do not have their errors appended to the formset's errors.

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