Skip to content

Commit

Permalink
add non-form & non-field errors to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
powderflask committed Oct 10, 2023
1 parent ad55e14 commit cd03d49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fmft/formset_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
FormMap = Callable[[object], django.forms.Form]


class BaseModelFormSet(django.forms.models.BaseModelFormSet):
"""Extends BaseModelFormSet to add non-form error report suited to formsets displayed in a table"""
def all_non_field_errors(self):
error_list = self.non_form_errors().copy()
for form in self:
error_list.extend(form.non_field_errors())
return error_list


# Queryset: get the paged data used by a bound table


Expand Down
11 changes: 11 additions & 0 deletions fmft/templates/fmft/form_non_field_errors_tr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{# Render any non-field form errors as a table row #}
{% if form.non_field_errors %}
<tr class="form-errors">
<td colspan="{% if colspan %}{{ colspan }}{% else %}{{ form.fields|length }}{% endif %}">
{% if form_error_title %}<h5>{{ form_error_title }}</h5>{% endif %}
<ul>
{{ form.non_field_errors|unordered_list }}
</ul>
</td>
</tr>
{% endif %}
3 changes: 3 additions & 0 deletions fmft/templates/fmft/tabular_formset.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
</thead>
<tbody>
{% for form in formset %}
{% if form_show_errors and not form.is_extra %}
{% include "fmft/form_non_field_errors_tr.html" %}
{% endif %}
<tr>
{% for field in form %}
<td>{% include 'fmft/form_field.html' %}</td>
Expand Down
2 changes: 2 additions & 0 deletions fmft/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class BaseModelFormSetView(
):
"""A Base class that emulates formsets.ModelFormSetView, but without its request
handlers"""
formset_class = formset_tables.BaseModelFormSet

pass

Expand Down Expand Up @@ -140,6 +141,7 @@ class BaseModelFormSetSingleTableMixin(BaseFormSetFactory, tables.SingleTableMix

model = None
request = None
formset_class = formset_tables.BaseModelFormSet
_table = None
_formset = None

Expand Down

0 comments on commit cd03d49

Please sign in to comment.