Kaydet (Commit) 1b7634a0 authored tarafından Baptiste Mispelon's avatar Baptiste Mispelon Kaydeden (comit) Tim Graham

Fixed #20464 -- Added a `total_error_count` method on formsets.

Thanks to frog32 for the report and to Tim Graham for the review.
üst aa22cbd5
......@@ -64,7 +64,7 @@
{% endblock %}
{% if cl.formset.errors %}
<p class="errornote">
{% if cl.formset.errors|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
{% if cl.formset.total_error_count == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
</p>
{{ cl.formset.non_form_errors }}
{% endif %}
......
......@@ -263,6 +263,13 @@ class BaseFormSet(object):
self.full_clean()
return self._errors
def total_error_count(self):
"""
Returns the number of errors across all forms in the formset.
"""
return len(self.non_form_errors()) +\
sum(len(form_errors) for form_errors in self.errors)
def _should_delete_form(self, form):
"""
Returns whether or not the form was marked for deletion.
......
......@@ -315,6 +315,9 @@ Minor features
:class:`~django.contrib.admin.InlineModelAdmin` may be overridden to
customize the extra and maximum number of inline forms.
* Formsets now have a
:meth:`~django.forms.formsets.BaseFormSet.total_error_count` method.
Backwards incompatible changes in 1.6
=====================================
......
......@@ -164,6 +164,23 @@ As we can see, ``formset.errors`` is a list whose entries correspond to the
forms in the formset. Validation was performed for each of the two forms, and
the expected error message appears for the second item.
.. currentmodule:: django.forms.formsets.BaseFormSet
.. method:: total_error_count(self)
.. versionadded:: 1.6
To check how many errors there are in the formset, we can use the
``total_error_count`` method::
>>> # Using the previous example
>>> formset.errors
[{}, {'pub_date': [u'This field is required.']}]
>>> len(formset.errors)
2
>>> formset.total_error_count()
1
We can also check if form data differs from the initial data (i.e. the form was
sent without any data)::
......
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