{# Field-level error message.
The id is what `aria-describedby` on the control points at, so the message
is read out when focus reaches the field rather than only being visible.
Errors also carry an icon + "Error:" prefix so the failure is not signalled
by the red colour alone (WCAG 1.4.1), and role="alert" so a message that
arrives in an Unpoly fragment swap is announced. #}
{% macro field_error(construct, error_id, error) %}
{% if error %}
{{ icon("cancel", label=construct.t("crud-error-prefix")) }}
{{ construct.t(error.key, error.args) }}
{% endif %}
{% endmacro %}
{# Attributes tying a control to its help text and error message. Emitted on
the control itself so the association survives however the field is laid
out. Help comes first so it is read before the error. #}
{% macro error_attrs(error_id, error, help_id=none) %}
{%- set ids = ([help_id] if help_id else []) + ([error_id] if error else []) -%}
{%- if error %} aria-invalid="true"{% endif -%}
{%- if ids %} aria-describedby="{{ ids | join(' ') }}"{% endif -%}
{% endmacro %}
{# A field's `help=` text: visible, persistent guidance placed between the
label and the control, and referenced by the control's aria-describedby
(pass the same id to `error_attrs`). Unlike a placeholder it stays put
while the user types and is read out by screen readers. Callers derive the
id as `name ~ '-help' if type.help else none`. #}
{% macro field_help(construct, help_id, type) %}
{% if type.help %}{{ construct.t(type.help) }}{% endif %}
{% endmacro %}
{# Placeholder for a text-like control: the field's own `placeholder=` when
set, otherwise the generic "enter ". #}
{% macro placeholder_text(construct, type) -%}
{%- if type.placeholder %}{{ construct.t(type.placeholder) }}{% else %}{{ construct.t("crud-enter-field", {"fieldname": construct.t(type.name) }).lower() }}{% endif -%}
{%- endmacro %}
{# A displayed value, with an empty one left blank rather than printed.
`format_value` hands back the stored attribute itself, which is None for a
null column and for a `Related` pointing at nothing — and Jinja renders that
as the word "None", so an unfilled field reads as though somebody typed it.
The test is against none rather than falsiness, because 0, an empty string
and False are all values somebody meant. #}
{# "Add " / "New " when the record name is known, else the
generic "Add new item" / "New item". #}
{% macro add_label(construct, recname) -%}
{%- if recname %}{{ construct.t("crud-add-rec", {"recname": construct.t(recname, {"count": 1})}) }}{% else %}{{ construct.t("crud-add-new-item") }}{% endif -%}
{%- endmacro %}
{% macro new_label(construct, recname) -%}
{%- if recname %}{{ construct.t("crud-new-rec", {"recname": construct.t(recname, {"count": 1})}) }}{% else %}{{ construct.t("crud-new-item") }}{% endif -%}
{%- endmacro %}
{% macro shown(value) %}{%- if value is not none %}{{ value }}{% endif -%}{% endmacro %}
{# File and image fields (schema.File / schema.Image; see files.py).
form_enctype: the attribute a