{# WTForms macros heavily inspired by Flask-Bootstrap. # Consult their docs if you are confused about anything here: # http://pythonhosted.org/Flask-Bootstrap/macros.html?highlight=quick_form#quick_form #} {# Render a flask.ext.wtforms.Form object. Parameters: form – The form to output. method –
method attribute (default 'POST') extra_classes – The classes to add to the . enctype – enctype attribute. If None, will automatically be set to multipart/form-data if a FileField is present in the form. #} {% macro render_form(form, method='POST', extra_classes='', enctype=None) %} {% set flashes = { 'error': get_flashed_messages(category_filter=['form-error']), 'warning': get_flashed_messages(category_filter=['form-check-email']), 'info': get_flashed_messages(category_filter=['form-info']), 'success': get_flashed_messages(category_filter=['form-success']) } %} {{ begin_form(form, flashes, method=method, extra_classes=extra_classes, enctype=enctype) }} {% for field in form if not (is_hidden_field(field) or field.type == 'SubmitField') %} {{ render_form_field(field) }} {% endfor %} {{ form_message(flashes['error'], header='Something went wrong.', class='error') }} {{ form_message(flashes['warning'], header='Check your email.', class='warning') }} {{ form_message(flashes['info'], header='Information', class='info') }} {{ form_message(flashes['success'], header='Success!', class='success') }} {% for field in form | selectattr('type', 'equalto', 'SubmitField') %} {{ render_form_field(field) }} {% endfor %} {{ end_form(form) }} {% endmacro %} {# Set up the form, including hidden fields and error states #} {% macro begin_form(form, flashes, method='POST', extra_classes='', enctype=None) %} {# Set proper enctype #} {% if enctype is none and (form | selectattr('type', 'equalto', 'FileField') | list | length > 0) %} {% set enctype = 'multipart/form-data' %} {% elif enctype is none and (form | selectattr('type', 'equalto', 'MultipleFileField') | list | length > 0) %} {% set enctype = 'multipart/form-data' %} {% else %} {% set enctype = '' %} {% endif %} {{ form.hidden_tag() }} {% endmacro %} {# Mirrors begin_form #} {% macro end_form(form) %}
{% endmacro %} {# Render a message for the form #} {% macro form_message(messages, header=none, class='') %} {% if messages %}
{% if header is not none %}
{{ header }}
{% endif %} {% if messages %} {% endif %}
{% endif %} {% endmacro %} {# Render a field for the form #} {% macro render_custom_select(field) %} {{ field.label }} {% if field.description %}
{{ field.description }}
{% endif %} {% if field.allow_custom %} {% endif %} {% endmacro %} {# Render a field for the form #} {% macro render_form_field(field, extra_classes='') %} {% if field.type == 'Radio Field' %} {% set extra_classes = extra_classes + ' grouped fields' %} {% endif %} {{ render_form_input(field) }} {% if field.errors %}
{{ field.errors[0] | safe }}
{% endif %} {% endmacro %} {% macro render_form_input(field) %} {% if field.widget.input_type == 'checkbox' %}
{{ field }} {{ field.label }}
{% elif field.type == 'RadioField' %} {{ field.label }} {% for item in field %}
{{ item }} {{ item.label }}
{% endfor %} {% elif field.type == 'SubmitField' %}
{{ field(class='btn btn-primary') }}
{% elif field.type == 'FormField' %} {{ render_form(field) }} {% elif field.type == 'SelectMultipleField' %} {% elif field.type == 'CustomSelectField' %} {{ render_custom_select(field) }} {% elif field.type == 'EmailField' %}
{{field.label}}
{{ field(placeholder=field.label.text) }}
{% if field.description %}
{{ field.description }}
{% endif %} {% else %}
{{field.label}}
{{ field(placeholder=field.label.text) }}
{% if field.description %}
{{ field.description }}
{% endif %} {% endif %} {% endmacro %}