Skip to content

Instantly share code, notes, and snippets.

@fsevestre
Created May 30, 2017 19:43
Show Gist options
  • Save fsevestre/45b19bd6790806e281690d5a5971268c to your computer and use it in GitHub Desktop.
Save fsevestre/45b19bd6790806e281690d5a5971268c to your computer and use it in GitHub Desktop.
[Symfony 2 / Bootstrap 3] Form errors in tooltips or popovers
{% extends 'bootstrap_3_layout.html.twig' %}
{% block form_row -%}
{% if (form_errors_in_tooltip is defined and form_errors_in_tooltip) or (form_errors_in_popover is defined and form_errors_in_popover) %}
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% else %}
{{ parent() }}
{% endif %}
{%- endblock form_row %}
{% block form_errors -%}
{% if errors|length > 0 -%}
{% if (form_errors_in_tooltip is defined and form_errors_in_tooltip) or (form_errors_in_popover is defined and form_errors_in_popover) %}
{% set error_message = '<ul class="list-unstyled">' %}
{%- for error in errors -%}
{% set error_message = error_message ~ '<li>' ~ error.message ~ '</li>' %}
{%- endfor -%}
{% set error_message = error_message ~ '</ul>' %}
{% set error_message = error_message|raw %}
{% if form_errors_in_tooltip is defined and form_errors_in_tooltip %}
<i class="fa fa-warning" data-toggle="tooltip" data-html="true" data-placement="top" title="{{ error_message }}"></i>
{% else %}
<i class="fa fa-warning" data-toggle="popover" data-html="true" data-placement="top" data-trigger="hover" title="Erreur(s)" data-content="{{ error_message }}"></i>
{%- endif %}
{% else %}
{{ parent() }}
{% endif %}
{%- endif %}
{%- endblock form_errors %}
...
{{ form_row(form.tooltip, { 'form_errors_in_tooltip': true }) }}
{{ form_row(form.popover, { 'form_errors_in_popover': true }) }}
{{ form_row(form.classic) }}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment