Fix form error rendering

This commit is contained in:
Tobias Kunze 2025-03-27 13:54:53 +01:00
parent a72358a854
commit 2c65646146
5 changed files with 18 additions and 31 deletions

View file

@ -17,10 +17,12 @@ class VerticalFormRenderer(TemplatesSetting):
form_template_name = "frontend/forms/form.html" form_template_name = "frontend/forms/form.html"
field_template_name = "frontend/forms/vertical_field.html" field_template_name = "frontend/forms/vertical_field.html"
def get_class_names(self, input_type): def get_class_names(self, field):
input_type = self.get_field_input_type(field)
errors = "is-invalid " if field.errors else ""
if input_type == "checkbox": if input_type == "checkbox":
return "form-check-input" return f"{errors}form-check-input"
return "form-control" return f"{errors}form-control"
def get_widget_input_type(self, widget): def get_widget_input_type(self, widget):
if isinstance(widget, Textarea): if isinstance(widget, Textarea):
@ -35,9 +37,8 @@ class VerticalFormRenderer(TemplatesSetting):
def render(self, template_name, context, request=None): def render(self, template_name, context, request=None):
if field := context.get("field"): if field := context.get("field"):
input_type = self.get_field_input_type(field)
field.build_widget_attrs = inject_class( field.build_widget_attrs = inject_class(
field.build_widget_attrs, self.get_class_names(input_type) field.build_widget_attrs, self.get_class_names(field)
) )
return super().render(template_name, context, request) return super().render(template_name, context, request)

View file

@ -1,5 +1,5 @@
{% load i18n %} {% load i18n %}
<div class="form-group{% if field.field.required %} mandatory{% endif %}{% if errors %} is-invalid{% endif %}{% if extra_class %} {{ extra_class }}{% endif %}"> <div class="form-group{% if field.field.required %} mandatory{% endif %}{% if field.errors %} is-invalid{% endif %}{% if extra_class %} {{ extra_class }}{% endif %}">
{% if not hide_label %} {% if not hide_label %}
{% if field.field.widget.input_type != "checkbox" or field.field.widget.allow_multiple_selected %} {% if field.field.widget.input_type != "checkbox" or field.field.widget.allow_multiple_selected %}
<label for="{{ field.auto_id }}" class="form-label">{{ field.label }}</label> <label for="{{ field.auto_id }}" class="form-label">{{ field.label }}</label>

View file

@ -1,12 +1,17 @@
{% if errors %} {% load i18n %}
{% if form.non_field_errors or form.errors %}
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
<div> <div>
{% if errors|length > 1 %} {% if form.non_field_errors %}
<ul> {% if form.non_field_errors|length > 1 %}
{% for error in errors %}<li>{{ error }}</li>{% endfor %} <ul>
</ul> {% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% else %}
{{ form.non_field_errors.0 }}
{% endif %}
{% else %} {% else %}
{{ errors.0 }} {% translate "We could not save your changes." %}
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -2,7 +2,6 @@
<form class="form form-vertical" <form class="form form-vertical"
method="post" method="post"
{% if form_action %}action="{{ form_action }}"{% endif %}> {% if form_action %}action="{{ form_action }}"{% endif %}>
{% include "includes/form_errors.html" %}
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form }}
{% if extra_field %}{{ extra_field }}{% endif %} {% if extra_field %}{{ extra_field }}{% endif %}

View file

@ -1,18 +0,0 @@
{% load i18n %}
{% if form.non_field_errors or form.errors %}
<div class="alert alert-danger" role="alert">
<div>
{% if form.non_field_errors %}
{% if form.non_field_errors|length > 1 %}
<ul>
{% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% else %}
{{ form.non_field_errors.0 }}
{% endif %}
{% else %}
{% translate "We could not save your changes." %}
{% endif %}
</div>
</div>
{% endif %}