{% load crispy_forms_field %}

<!--DEBUG: formset.errors: {{ formset.errors }}-->
<!--DEBUG: formset.non_field_errors: {{ formset.non_field_errors }}-->

{% if formset.errors or formset.non_form_errors %}
  <div class="govuk-error-summary"
       aria-labelledby="error-summary-title"
       role="alert"
       tabindex="-1"
       data-module="govuk-error-summary">

      {% if formset_error_title %}
        <h2 class="govuk-error-summary__title" id="error-summary-title">
            {{ formset_error_title }}
        </h2>
      {% endif %}

    <div class="govuk-error-summary__body">
      <ul class="govuk-list govuk-error-summary__list">

          {# Non-form Errors #}

          {# Note these are errors relate to the formset as a whole #}
          {% for error in formset.non_form_errors %}
            <li>
                {{ error }}
            </li>
          {% endfor %}

          {% for form in formset %}
              {% for field in form %}
                  {% if field.errors %}
                      {% for error in field.errors %}
                          {# Note these are errors relate to the form as a whole #}
                          {% for error in form.non_field_errors %}
                            <li>
                                {{ error }}
                            </li>
                          {% endfor %}

                          <li>
                            <a href="#{{ field.auto_id }}{% if field|is_radioselect %}_0_1{% endif %}"> {{ error }}</a>
                          </li>
                      {% endfor %}
                  {% endif %}
              {% endfor %}
          {% endfor %}

      </ul>
    </div>
  </div>
{% endif %}
