{# app/templates/_form_helpers.html #}
{% macro render_field(field, label_visible=true) %}
  <div class="form-group mb-3">
    {% if field.type == "BooleanField" %}
      <div class="form-check">
        {{ field(class="form-check-input" + (" is-invalid" if field.errors else ""), **kwargs) }}
        {% if label_visible and field.label %}
          <label class="form-check-label" for="{{ field.id }}">{{ field.label.text }}</label>
        {% endif %}
        {% if field.description %}
          <small class="form-text text-muted">{{ field.description }}</small>
        {% endif %}
        {% if field.errors %}
          <div class="invalid-feedback">
            {% for error in field.errors %}
              {{ error }}<br>
            {% endfor %}
          </div>
        {% endif %}
      </div>
    {% else %}
      {% if label_visible and field.label %}
        {{ field.label(class="form-label") }}
      {% endif %}
      {% set css_class = 'form-control ' + kwargs.pop('class', '') %}
      {% if field.errors %}
        {% set css_class = css_class + ' is-invalid' %}
      {% endif %}
      {{ field(class=css_class, **kwargs) }}
      {% if field.description %}
        <small class="form-text text-muted">{{ field.description }}</small>
      {% endif %}
      {% if field.errors %}
        <div class="invalid-feedback">
          {% for error in field.errors %}
            {{ error }}<br>
          {% endfor %}
        </div>
      {% endif %}
    {% endif %}
  </div>
{% endmacro %}