Fix form error rendering
This commit is contained in:
parent
a72358a854
commit
2c65646146
5 changed files with 18 additions and 31 deletions
|
@ -17,10 +17,12 @@ class VerticalFormRenderer(TemplatesSetting):
|
|||
form_template_name = "frontend/forms/form.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":
|
||||
return "form-check-input"
|
||||
return "form-control"
|
||||
return f"{errors}form-check-input"
|
||||
return f"{errors}form-control"
|
||||
|
||||
def get_widget_input_type(self, widget):
|
||||
if isinstance(widget, Textarea):
|
||||
|
@ -35,9 +37,8 @@ class VerticalFormRenderer(TemplatesSetting):
|
|||
|
||||
def render(self, template_name, context, request=None):
|
||||
if field := context.get("field"):
|
||||
input_type = self.get_field_input_type(field)
|
||||
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)
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
{% 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 field.field.widget.input_type != "checkbox" or field.field.widget.allow_multiple_selected %}
|
||||
<label for="{{ field.auto_id }}" class="form-label">{{ field.label }}</label>
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
{% if errors %}
|
||||
{% load i18n %}
|
||||
{% if form.non_field_errors or form.errors %}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<div>
|
||||
{% if errors|length > 1 %}
|
||||
<ul>
|
||||
{% for error in errors %}<li>{{ error }}</li>{% endfor %}
|
||||
</ul>
|
||||
{% 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 %}
|
||||
{{ errors.0 }}
|
||||
{% translate "We could not save your changes." %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
<form class="form form-vertical"
|
||||
method="post"
|
||||
{% if form_action %}action="{{ form_action }}"{% endif %}>
|
||||
{% include "includes/form_errors.html" %}
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
{% if extra_field %}{{ extra_field }}{% endif %}
|
||||
|
|
|
@ -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 %}
|
Loading…
Add table
Add a link
Reference in a new issue