Files
ephub/templates/forms.html.twig
T

103 lines
4.6 KiB
Twig

{#
Ported from myep's forms_admin.html.twig, trimmed to the blocks this app actually has.
Registered globally in config/packages/twig.yaml rather than per-template: myep applies it
with {% form_theme %} because it carries two themes, we only have one.
Deliberately not ported: datepicker_widget, autocomplete_widget, multiselect_widget,
money_widget, birthday_widget, abbreviated_date_widget -- all back myep-specific form types
and Stimulus controllers that do not exist here.
#}
{% use 'form_div_layout.html.twig' %}
{%- block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~ "_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
{{- form_label(form) -}}
{{- form_widget(form, widget_attr) -}}
{{- form_errors(form) -}}
{{- form_help(form) -}}
</div>
{%- endblock form_row -%}
{%- block form_label -%}
{% set class = 'block font-bold pb-2' %}
{% if errors|length %}
{% set class = class ~ ' text-red-500' %}
{% endif %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' ' ~ class)|trim}) %}
{{ parent() }}
{%- endblock form_label -%}
{%- block form_errors -%}
{%- if errors|length > 0 -%}
<ul class="pb-2">
{%- for error in errors -%}
<li class="text-red-500">{{ error.message }}</li>
{%- endfor -%}
</ul>
{%- endif -%}
{%- endblock form_errors -%}
{# The field class is repeated in each block on purpose: a top-level {% set %} in a form theme
is not in scope inside its blocks, because each block is rendered with the caller's context. #}
{%- block form_widget_simple -%}
{%- set type = type|default('text') -%}
{%- if type != 'hidden' -%}
{%- set attr = attr|merge({'class': (attr.class|default('') ~ ' block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset text-sm')|trim }) -%}
{%- if errors|length -%}
{%- set attr = attr|merge({'class': attr.class ~ ' ring-red-500 placeholder-red-500 focus:ring-red-500' }) -%}
{%- else -%}
{%- set attr = attr|merge({'class': attr.class ~ ' placeholder:text-gray-400 focus:ring-primary' }) -%}
{%- endif -%}
{%- if disabled is defined and disabled -%}
{%- set attr = attr|merge({'class': attr.class ~ ' cursor-not-allowed' }) -%}
{%- endif -%}
{%- endif -%}
{{ parent() }}
{%- endblock form_widget_simple -%}
{%- block textarea_widget -%}
{%- set attr = attr|merge({'class': (attr.class|default('') ~ ' block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset text-sm')|trim }) -%}
{%- if errors|length -%}
{%- set attr = attr|merge({'class': attr.class ~ ' ring-red-500 placeholder-red-500 focus:ring-red-500' }) -%}
{%- else -%}
{%- set attr = attr|merge({'class': attr.class ~ ' placeholder:text-gray-400 focus:ring-primary' }) -%}
{%- endif -%}
{{ parent() }}
{%- endblock textarea_widget -%}
{%- block choice_widget_collapsed -%}
{%- set attr = attr|merge({'class': (attr.class|default('') ~ ' block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset text-sm')|trim }) -%}
{%- if errors|length -%}
{%- set attr = attr|merge({'class': attr.class ~ ' ring-red-500 focus:ring-red-500' }) -%}
{%- else -%}
{%- set attr = attr|merge({'class': attr.class ~ ' focus:ring-primary' }) -%}
{%- endif -%}
{{ parent() }}
{%- endblock choice_widget_collapsed -%}
{%- block checkbox_widget -%}
{%- set attr = attr|merge({'class': (attr.class|default('') ~ ' h-4 w-4 rounded text-primary')|trim }) -%}
{%- if errors|length -%}
{%- set attr = attr|merge({'class': attr.class ~ ' border-red-500 text-red-500 focus:ring-red-500' }) -%}
{%- else -%}
{%- set attr = attr|merge({'class': attr.class ~ ' border-gray-300 focus:ring-primary' }) -%}
{%- endif -%}
{{ parent() }}
{%- endblock checkbox_widget -%}
{% block checkbox_row %}
<div class="relative flex items-start">
<div class="flex h-6 items-center">
{{ form_widget(form) }}
</div>
<div class="ml-3 leading-6 {{ errors|length ? 'text-red-500' : '' }}">
<label for="{{ form.vars.id }}">{{ form.vars.label }}</label>
{{- form_errors(form) -}}
</div>
</div>
{% endblock %}