nomilo/templates/macros/form.html
2025-06-22 18:01:06 +01:00

52 lines
1.7 KiB
HTML

{% macro input(id, name, value, type="text", description="", errors, errors_keys, error_msg_id, label, label_attributes, attributes) %}
{% set filtered_errors = [] %}
{% for error_key in errors_keys %}
{% set error = errors | get(key=error_key, default="") %}
{% if error %}
{% set_global filtered_errors = filtered_errors | concat(with=error) %}
{% endif %}
{% endfor %}
<label
for="{{ id }}"
{% for i in range(start=0, end=label_attributes|length, step_by=2) %}
{{ label_attributes[i] }}="{{ label_attributes | nth(n=i+1) }}"
{% endfor %}
>
{{ label }}
</label>
<div>
<input
{% if type == "number" %}
type="text"
inputmode="numeric"
{% else %}
type="{{ type }}"
{% endif %}
name="{{ name }}"
id="{{ id }}"
{% for i in range(start=0, end=attributes|length, step_by=2) %}
{{ attributes[i] }}="{{ attributes | nth(n=i+1) }}"
{% endfor %}
aria-describedby="{% for error in filtered_errors %} {{ id }}-error-{{ loop.index0 }}{% endfor %}{% if description %} {{ id }}-description{%endif %}"
{% if filtered_errors %}aria-invalid="true"{% endif %}
value="{{ value }}"
>
{% for error in filtered_errors %}
<p class="error" id="{{ id }}-error-{{ loop.index0 }}" data-new-item-skip>
{{ tr(
msg=error_msg_id,
attr="error-" ~ error.code | replace(from=":", to="-"),
lang=lang) }}
</p>
{% endfor %}
{% if description %}
<p class="help" id="{{ id }}-description">
{{ description }}
</p>
{% endif %}
</div>
{% endmacro %}