{% load widget_tweaks %}

{% if field.is_hidden %}
    {{ field }}
{% else %}
    {% comment %}
        Make the field widget type available to templates so we can mark-up
        checkbox and radio inputs differently to other widgets.
    {% endcomment %}

    {% block control_group %}
        <div class="form-group{% if style == "horizontal" %} row{% endif %}">

            {% block label %}
                {% if not nolabel and field.widget_type != 'checkbox' %}
                    <label for="{{ field.auto_id }}" class="{% if style|default:"stacked" != 'stacked' %}col-sm-4{% endif %} col-form-label{% if field.field.required %} required{% endif %}">
                        {{ field.label|safe }}
                    </label>
                {% endif %}
            {% endblock %}

            {% block controls %}
                <div class="{% if style|default:"stacked" != 'stacked' %}col-sm-7{% endif %}{% if field.widget_type == 'checkbox' %} form-check{% endif %}">
                    {% block widget %}
                        {% if field.widget_type == 'checkbox' %}
                            <label for="{{ field.auto_id }}" class="col-form-label {% if field.field.required %}required{% endif %}">
                                {% render_field field %}
                                {{ field.label|safe }}
                            </label>
                        {% else %}
                            {% render_field field class+="form-control" %}
                        {% endif %}
                    {% endblock %}

                    {% block errors %}
                        {% for error in field.errors %}
                            <span class="error-block"><i class="fas fa-exclamation"></i> {{ error }}</span>
                        {% endfor %}
                    {% endblock %}

                    {% block help_text %}
                        {% if field.help_text %}
                            <small class="form-text text-muted">
                                {# We allow HTML within form help fields #}
                                {{ field.help_text|safe }}
                            </small>
                        {% endif %}
                    {% endblock %}
                </div>
            {% endblock %}
        </div>
    {% endblock %}
{% endif %}
