Skip to content

Instantly share code, notes, and snippets.

@mcescalante
Created January 29, 2015 17:36
Show Gist options
  • Save mcescalante/a87aa5f715d15b83541b to your computer and use it in GitHub Desktop.
Save mcescalante/a87aa5f715d15b83541b to your computer and use it in GitHub Desktop.
jinja macro for rendering form field errors
{% macro with_errors(field) %}
<div class="form_field">
{% if field.errors %}
{% set css_class = 'has_error ' + kwargs.pop('class', '') %}
{{ field(class=css_class, **kwargs) }}
<ul class="errors">{% for error in field.errors %}<li>{{ error|e }}</li>{% endfor %}</ul>
{% else %}
{{ field(**kwargs) }}
{% endif %}
</div>
{% endmacro %}
Usage: {{ with_errors(form.field, style='font-weight: bold') }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment