From cd03d49b711a32efb60819f02e819837e3450b89 Mon Sep 17 00:00:00 2001 From: powderflask Date: Mon, 9 Oct 2023 17:10:45 -0700 Subject: [PATCH] add non-form & non-field errors to templates --- fmft/formset_tables.py | 9 +++++++++ fmft/templates/fmft/form_non_field_errors_tr.html | 11 +++++++++++ fmft/templates/fmft/tabular_formset.html | 3 +++ fmft/views.py | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 fmft/templates/fmft/form_non_field_errors_tr.html diff --git a/fmft/formset_tables.py b/fmft/formset_tables.py index d2e0ef3..a677f72 100644 --- a/fmft/formset_tables.py +++ b/fmft/formset_tables.py @@ -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 diff --git a/fmft/templates/fmft/form_non_field_errors_tr.html b/fmft/templates/fmft/form_non_field_errors_tr.html new file mode 100644 index 0000000..06d1a97 --- /dev/null +++ b/fmft/templates/fmft/form_non_field_errors_tr.html @@ -0,0 +1,11 @@ +{# Render any non-field form errors as a table row #} +{% if form.non_field_errors %} + + + {% if form_error_title %}
{{ form_error_title }}
{% endif %} + + + +{% endif %} \ No newline at end of file diff --git a/fmft/templates/fmft/tabular_formset.html b/fmft/templates/fmft/tabular_formset.html index 2efa07d..5dedf38 100644 --- a/fmft/templates/fmft/tabular_formset.html +++ b/fmft/templates/fmft/tabular_formset.html @@ -16,6 +16,9 @@ {% for form in formset %} + {% if form_show_errors and not form.is_extra %} + {% include "fmft/form_non_field_errors_tr.html" %} + {% endif %} {% for field in form %} {% include 'fmft/form_field.html' %} diff --git a/fmft/views.py b/fmft/views.py index 913b942..8104499 100644 --- a/fmft/views.py +++ b/fmft/views.py @@ -83,6 +83,7 @@ class BaseModelFormSetView( ): """A Base class that emulates formsets.ModelFormSetView, but without its request handlers""" + formset_class = formset_tables.BaseModelFormSet pass @@ -140,6 +141,7 @@ class BaseModelFormSetSingleTableMixin(BaseFormSetFactory, tables.SingleTableMix model = None request = None + formset_class = formset_tables.BaseModelFormSet _table = None _formset = None