Kaydet (Commit) 04e0fc02 authored tarafından Andrew Godwin's avatar Andrew Godwin

Merge pull request #1094 from senko/ticket_11160

Fixed #11160: Formset non_form_errors returns ErrorList() if is_valid is not called
...@@ -474,6 +474,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -474,6 +474,7 @@ answer newbie questions, and generally made Django that much better:
Luciano Ramalho Luciano Ramalho
Amit Ramon <amit.ramon@gmail.com> Amit Ramon <amit.ramon@gmail.com>
Philippe Raoult <philippe.raoult@n2nsoft.com> Philippe Raoult <philippe.raoult@n2nsoft.com>
Senko Rašić <senko.rasic@dobarkod.hr>
Massimiliano Ravelli <massimiliano.ravelli@gmail.com> Massimiliano Ravelli <massimiliano.ravelli@gmail.com>
Brian Ray <http://brianray.chipy.org/> Brian Ray <http://brianray.chipy.org/>
Lee Reilly <lee@leereilly.net> Lee Reilly <lee@leereilly.net>
......
...@@ -250,9 +250,9 @@ class BaseFormSet(object): ...@@ -250,9 +250,9 @@ class BaseFormSet(object):
form -- i.e., from formset.clean(). Returns an empty ErrorList if there form -- i.e., from formset.clean(). Returns an empty ErrorList if there
are none. are none.
""" """
if self._non_form_errors is not None: if self._non_form_errors is None:
return self._non_form_errors self.full_clean()
return self.error_class() return self._non_form_errors
@property @property
def errors(self): def errors(self):
...@@ -291,9 +291,12 @@ class BaseFormSet(object): ...@@ -291,9 +291,12 @@ class BaseFormSet(object):
def full_clean(self): def full_clean(self):
""" """
Cleans all of self.data and populates self._errors. Cleans all of self.data and populates self._errors and
self._non_form_errors.
""" """
self._errors = [] self._errors = []
self._non_form_errors = self.error_class()
if not self.is_bound: # Stop further processing. if not self.is_bound: # Stop further processing.
return return
for i in range(0, self.total_form_count()): for i in range(0, self.total_form_count()):
......
...@@ -972,6 +972,20 @@ class FormsFormsetTestCase(TestCase): ...@@ -972,6 +972,20 @@ class FormsFormsetTestCase(TestCase):
finally: finally:
formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM formsets.DEFAULT_MAX_NUM = _old_DEFAULT_MAX_NUM
def test_non_form_errors_run_full_clean(self):
# Regression test for #11160
# If non_form_errors() is called without calling is_valid() first,
# it should ensure that full_clean() is called.
class BaseCustomFormSet(BaseFormSet):
def clean(self):
raise ValidationError("This is a non-form error")
ChoiceFormSet = formset_factory(Choice, formset=BaseCustomFormSet)
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
self.assertTrue(isinstance(formset.non_form_errors(), ErrorList))
self.assertEqual(list(formset.non_form_errors()),
['This is a non-form error'])
data = { data = {
'choices-TOTAL_FORMS': '1', # the number of forms rendered 'choices-TOTAL_FORMS': '1', # the number of forms rendered
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment