{% load i18n l10n tbxforms %}

<fieldset
    class="tbxforms-fieldset"
    {% if field.help_text or field.errors %}
        aria-describedby="{% for error in field.errors %}{{ field.auto_id }}_{{ forloop.counter }}_error {% endfor %}{% if field.help_text %}{{ field.auto_id }}_hint{% endif %}"
    {% endif %}
>

    {% if field.label %}
        <legend class="tbxforms-fieldset__legend{% if legend_size %} {{ legend_size }}{% endif %}">
            {% if legend_tag %}<{{ legend_tag }} class="tbxforms-fieldset__heading">{% endif %}
            {{ field.label|safe }}
            {% if not field|show_as_required %} <span>{% trans "(optional)" %}</span>{% endif %}
            {% if legend_tag %}</{{ legend_tag }}>{% endif %}
        </legend>
    {% endif %}

    {% include 'tbx/layout/help_text_and_errors.html' %}

    <div class="tbxforms-checkboxes{% if inline %}--inline{% endif %}{% if checkboxes_small %} tbxforms-checkboxes--small{% endif %}">
        {% for choice in field.field.choices %}
            <div class="tbxforms-checkboxes__item">

                <input
                    type="checkbox"
                    name="{{ field.html_name }}"
                    class="tbxforms-checkboxes__input"
                    id="id_{{ field.html_name }}_{{ forloop.counter }}"
                    value="{{ choice.0|unlocalize }}"
                    {% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %}
                        checked="checked"
                    {% endif %}
                    {% if choice.hint %}
                        aria-describedby="id_{{ field.html_name }}_{{ forloop.counter }}_hint"
                    {% endif %}
                >

                <label class="tbxforms-label tbxforms-checkboxes__label" for="id_{{ field.html_name }}_{{ forloop.counter }}">
                    {{ choice.1|unlocalize }}
                </label>

                {% if choice.hint %}
                    <span id="id_{{ field.html_name }}_{{ forloop.counter }}_hint" class="tbxforms-hint tbxforms-checkboxes__hint">
                        {{ choice.hint }}
                    </span>
                {% endif %}
            </div>
        {% endfor %}
    </div>

</fieldset>
