0
0
mirror of https://github.com/django/django.git synced 2024-11-29 22:56:46 +01:00

Refs #29113 -- Simplified formset validation.

Thanks Nick Pope for review and complement.
This commit is contained in:
Claude Paroz 2020-11-03 09:57:10 +01:00 committed by GitHub
parent 92434bb0f5
commit b03b19b585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -306,17 +306,14 @@ class BaseFormSet:
"""Return True if every form in self.forms is valid.""" """Return True if every form in self.forms is valid."""
if not self.is_bound: if not self.is_bound:
return False return False
# We loop over every form.errors here rather than short circuiting on the # Accessing errors triggers a full clean the first time only.
# first failure to make sure validation gets triggered for every form.
forms_valid = True
# This triggers a full clean.
self.errors self.errors
for form in self.forms: # List comprehension ensures is_valid() is called for all forms.
if self.can_delete and self._should_delete_form(form): # Forms due to be deleted shouldn't cause the formset to be invalid.
# This form is going to be deleted so any of its errors forms_valid = all([
# shouldn't cause the entire formset to be invalid. form.is_valid() for form in self.forms
continue if not (self.can_delete and self._should_delete_form(form))
forms_valid &= form.is_valid() ])
return forms_valid and not self.non_form_errors() return forms_valid and not self.non_form_errors()
def full_clean(self): def full_clean(self):
@ -470,7 +467,5 @@ def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
def all_valid(formsets): def all_valid(formsets):
"""Validate every formset and return True if all are valid.""" """Validate every formset and return True if all are valid."""
valid = True # List comprehension ensures is_valid() is called for all formsets.
for formset in formsets: return all([formset.is_valid() for formset in formsets])
valid &= formset.is_valid()
return valid