Clean up new has_field_errors mechanism, fix issue with ObjectAttribute

This commit is contained in:
Jason Novinger 2025-05-16 11:32:51 -05:00
parent a3adb6de48
commit cb82810b27
2 changed files with 10 additions and 5 deletions

View File

@ -6,7 +6,7 @@
<h2 class="col-9 offset-3">{{ heading }}</h2>
</div>
{% endif %}
{% for layout, title, items, has_errors in rows %}
{% for layout, title, items, has_field_errors in rows %}
{% if layout == 'field' %}
{# Single form field #}
@ -25,7 +25,7 @@
{% elif layout == 'inline' %}
{# Multiple form fields on the same line #}
<div class="row mb-3 {% if has_errors %} has-errors{% endif %}">
<div class="row mb-3 {% if has_field_errors %} has-errors{% endif %}">
<label class="col col-3 col-form-label text-lg-end">{{ title|default:'' }}</label>
{% for field in items %}
<div class="col mb-1 {% if field.errors %} has-errors{% endif %}">

View File

@ -51,6 +51,8 @@ def widget_type(field):
def render_fieldset(form, fieldset):
"""
Render a group set of fields.
The signature for row tuples is (layout, title, items, has_field_errors).
"""
rows = []
@ -75,18 +77,21 @@ def render_fieldset(form, fieldset):
'fields': [form[name] for name in tab['fields'] if name in form.fields]
} for tab in item.tabs
]
has_field_errors = any(
field.errors for tab in tabs for field in tab['fields']
)
# If none of the tabs has been marked as active, activate the first one
if not any(tab['active'] for tab in tabs):
tabs[0]['active'] = True
rows.append(
('tabs', None, tabs, False)
('tabs', None, tabs, has_field_errors)
)
elif type(item) is ObjectAttribute:
value = getattr(form.instance, item.name)
label = value._meta.verbose_name if hasattr(value, '_meta') else item.name
rows.append(
('attribute', label.title(), [value])
('attribute', label.title(), [value], False)
)
# A single form field
@ -96,7 +101,7 @@ def render_fieldset(form, fieldset):
if field.name in getattr(form, 'nullable_fields', []):
field._nullable = True
rows.append(
('field', None, [field], False)
('field', None, [field], bool(field.errors))
)
return {