{% if not field.validate.ignore %}

{% set field_name = (scope ~ field.name)|fieldName %}
{% set vertical = field.style == 'vertical' %}

{% if not blueprints or (blueprints.schema.type(field.type)['input@'] ?? true) is same as(true) %}
    {% set toggleable = field.toggleable ?? false %}
    {% if toggleable %}
        {% set originalValue = originalValue is defined ? originalValue : value %}
        {% set toggleableChecked = originalValue is not null and originalValue is not empty %}
    {% elseif field.overridable %}
        {% set toggleable = true %}
        {% set default = form.getDefaultValue(field.name) ?? field.default %}
        {% set toggleableChecked = value != default %}
    {% endif %}

    {% set cookie_name = 'forms-' ~ form.name ~ '-' ~ field.name %}
    {% set value = value ?? (get_cookie(cookie_name) is not null ? get_cookie(cookie_name) : field.default) %}

    {% if (field.yaml or field.validate.type == 'yaml') and value is iterable %}
        {% set value = value|toYaml %}
    {% endif %}
{% else %}
    {% set toggleable = false %}
{% endif %}

{# DEPRECATED: Needed by old form fields; remove when backwards compatibility breaks are allowed #}
{% set isDisabledToggleable = toggleable and not toggleableChecked %}

{% set errors = attribute(form.messages, field.name) %}
{% set required = client_side_validation and field.validate.required in ['on', 'true', 1] %}
{% set autofocus = (inline_errors == false) and field.autofocus in ['on', 'true', 1] %}

{% if inline_errors and errors %}
    {% set autofocus = true %}
{% endif %}

{% block field %}
    <div class="form-field {{ form_field_outer_classes }} {{ field.outerclasses }} {% if errors %} has-errors{% endif %} {% block outer_field_classes %}{% endblock %} {% if toggleable %} form-field-toggleable{% endif %}">
        {% block contents %}
            {% if field.label is not same as(false) and field.display_label is not same as(false) %}
                <div class="{{ form_field_outer_label_classes ?: 'form-label' }} {{ field.labelclasses }}">
                    {% if toggleable %}
                        {% include 'forms/default/toggleable.html.twig' with {field_name: field_name, field: field, checked: toggleableChecked} %}
                    {% endif %}
                    <label class="{{ form_field_label_classes ?: 'inline' }} {{ toggleable ? 'toggleable' }}" {% if field.id is defined %}for="{{ toggleable ? 'toggleable_' ~ field.name : field.id|e }}" {% endif %} >
                    {% block label %}
                        {% if field.help %}
                            {% if field.markdown %}
                                <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|markdown(false)|e }}">{{ field.label|markdown(false)|default(field.name|capitalize)|t }}</span>
                            {% else %}
                                <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|e }}">{{ field.label|default(field.name|capitalize)|t }}</span>
                            {% endif %}
                        {% else %}
                            {% if field.markdown %}
                                {{ field.label|markdown(false)|default(field.name|capitalize)|t }}
                            {% else %}
                                {{ field.label|default(field.name|capitalize)|t|e('html_attr') }}
                            {% endif %}
                        {% endif %}

                        {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
                    {% endblock %}
                    </label>
                </div>
            {% endif %}
            <div class="{{ form_field_outer_data_classes ?: 'form-data' }} {{ field.dataclasses }}"
                {% block global_attributes %}
                data-grav-field="{{ field.type }}"
                data-grav-disabled="{{ toggleable and toggleableChecked }}"
                data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
                {% endblock %}
            >
                {% block group %}
                    {% block input %}
                        <div class="{{ form_field_wrapper_classes ?: 'form-input-wrapper' }} {{ field.size }} {{ field.wrapper_classes }}">
                            {% block prepend %}{% endblock prepend %}
                            <input
                                {# required attribute structures #}
                                name="{{ (scope ~ field.name)|fieldName }}"
                                value="{{ value|join(', ')|e('html_attr') }}"
                                {# input attribute structures #}
                                {% block input_attributes %}
                                    class="{{ form_field_input_classes }} {{ field.classes }} {{ field.size }}"
                                    {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
                                    {% if field.style is defined %}style="{{ field.style|e }}" {% endif %}
                                    {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
                                    {% if field.placeholder %}placeholder="{{ field.placeholder|t|e('html_attr') }}"{% endif %}
                                    {% if autofocus %}autofocus="autofocus"{% endif %}
                                    {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
                                    {% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
                                    {% if field.autocomplete is defined %}autocomplete="{{ field.autocomplete }}"{% endif %}
                                    {% if field.autocapitalize in ['off', 'characters', 'words', 'sentences'] %}autocapitalize="{{ field.autocapitalize }}"{% endif %}
                                    {% if field.inputmode in ['none', 'text', 'decimal', 'numeric', 'tel', 'search', 'email', 'url'] %}inputmode="{{ field.inputmode }}"{% endif %}
                                    {% if field.spellcheck in ['true', 'false'] %}spellcheck="{{ field.spellcheck }}"{% endif %}

                                    {% if field.attributes is defined %}
                                      {% for attribute in field.attributes %}
                                          {{ attribute.name }}="{{ attribute.value|e }}"
                                      {% endfor %}
                                    {% endif %}
                                    {% if required %}required="required"{% endif %}
                                    {% if field.validate.pattern %}pattern="{{ field.validate.pattern|e }}"{% endif %}
                                    {% if field.validate.message %}title="{{ field.validate.message|t|e }}"
                                    {% elseif field.title is defined %}title="{{ field.title|t|e }}" {% endif %}
                                    {% if field.datasets %}
                                        {% for datakey, datavalue in field.datasets %}
                                            data-{{ datakey }}="{{ datavalue|e('html_attr') }}"
                                        {% endfor %}
                                    {% endif %}
                                {% endblock %}
                            />
                            {% block append %}{% endblock append %}
                            {% if inline_errors and errors %}
                                <div class="{{ form_errors_classes ?: 'form-errors' }}">
                                  <p class="form-message"><i class="fa fa-exclamation-circle"></i> {{ errors|first }}</p>
                                </div>
                            {% endif %}
                        </div>
                    {% endblock %}
                {% endblock %}
                {% if field.description %}
                    <div class="form-extra-wrapper {{ field.wrapper_classes }}">
                        <span class="form-description">
                            {% if field.markdown %}
                                {{ field.description|t|markdown(false)|raw }}
                            {% else %}
                                {{ field.description|t|raw }}
                            {% endif %}
                        </span>
                    </div>
                {% endif %}

            </div>
        {% endblock %}
    </div>
{% endblock %}

{% endif %}
