{# 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 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.has_help() else none`.
A field typed in a set form (a Date with input="text") adds that form,
"Format: dd/mm/yyyy", to it, so it is shown and read out with the field.
It goes between the label and the control ("above", the default) or after
the control and any suffix ("below"), per the field's `help_position` or
else CONSTRUCT_FIELD_HELP_POSITION. A widget calls this twice, with
at="above" before the control and at="below" after it; only the call that
matches renders, so the id is never doubled. at=none renders regardless,
for spots that have only one place for it (a checkbox's help, a column
header). #}
{% macro field_help(construct, help_id, type, at="above") %}
{%- set format = type.input_format() -%}
{%- if (type.help or format) and (at is none or (type.help_position or construct.field_help_position) == at) -%}
{%- if type.help %}{{ construct.t(type.help) }}{% endif -%}
{%- if format %}{% if type.help %} {% endif %}{{ construct.t("crud-date-format", {"format": format}) }}{% endif -%}
{%- endif -%}
{% endmacro %}
{# The whole placeholder attribute for a text-like control, or nothing. The
field's own `placeholder=` when set; `placeholder=""` switches it off; with
none set, the generic "enter " only when CONSTRUCT_DEFAULT_PLACEHOLDER
is on (off by default: a placeholder that repeats the label adds nothing,
fails contrast and reads as a filled-in value). #}
{% macro placeholder_attr(construct, type) -%}
{%- if type.placeholder is not none -%}
{%- if type.placeholder != "" %}placeholder="{{ construct.t(type.placeholder) }}"{% endif -%}
{%- elif construct.default_placeholder -%}
placeholder="{{ 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_rec("crud-add-rec", recname) }}{% else %}{{ construct.t("crud-add-new-item") }}{% endif -%}
{%- endmacro %}
{% macro new_label(construct, recname) -%}
{%- if recname %}{{ construct.t_rec("crud-new-rec", recname) }}{% else %}{{ construct.t("crud-new-item") }}{% endif -%}
{%- endmacro %}
{# The record's tab bar: its own form first (labelled by
view.details_tab_name, else the entity name, with the icon
view.details_tab_icon, none when it is None), then its @tab_action tabs.
The edit page and every tab page share it, so the labels stay put as the
reader moves between tabs. #}
{% macro record_tabs(construct, view, id, tab_actions, active_tab) -%}
{%- 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