{# This file was part of Flask-Bootstrap and was modified under the terms of its BSD License. Copyright (c) 2013, Marc Brinkmann. All rights reserved. #} {% macro form_errors(form, hiddens=True) %} {%- if form.errors %} {%- for fieldname, errors in form.errors.items() %} {%- if bootstrap_is_hidden_field(form[fieldname]) and hiddens or not bootstrap_is_hidden_field(form[fieldname]) and hiddens != 'only' %} {%- for error in errors %}
{{ error }}
{%- endfor %} {%- endif %} {%- endfor %} {%- endif %} {%- endmacro %} {% macro _hz_form_wrap(horizontal_columns, form_type, add_group=False, required=False) %} {% if form_type == "horizontal" %} {% if add_group %}
{% endif %}
{% endif %} {{ caller() }} {% if form_type == "horizontal" %} {% if add_group %}
{% endif %}
{% endif %} {% endmacro %} {% macro render_field(field, form_type="basic", horizontal_columns=('lg', 2, 10), button_map={}) %} {# this is a workaround hack for the more straightforward-code of just passing required=required parameter. older versions of wtforms do not have the necessary fix for required=False attributes, but will also not set the required flag in the first place. we skirt the issue using the code below #} {% if field.flags.required and not required in kwargs %} {% set kwargs = dict(required=True, **kwargs) %} {% endif %} {% if field.widget.input_type == 'checkbox' %} {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
{% endcall %} {%- elif field.type == 'RadioField' -%} {# note: A cleaner solution would be rendering depending on the widget, this is just a hack for now, until I can think of something better #}
{%- if form_type == "inline" %} {{ field.label(class="sr-only")|safe }} {% elif form_type == "horizontal" %} {{ field.label(class="form-control-label " + ( " col-%s-%s" % horizontal_columns[0:2]))|safe }} {%- else -%} {{ field.label(class="form-control-label")|safe }} {% endif %} {% if form_type == 'horizontal' %}
{% endif %} {#% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %#} {% for item in field -%}
{% endfor %} {#% endcall %#} {% if form_type == 'horizontal' %}
{% endif %}
{%- elif field.type == 'SubmitField' -%} {# deal with jinja scoping issues? #} {% set field_kwargs = kwargs %} {# note: same issue as above - should check widget, not field type #} {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %} {{ field(class='btn btn-%s' % button_map.get(field.name, 'secondary'), **field_kwargs) }} {% endcall %} {%- elif field.type in ['FormField', 'FieldList'] -%} {# note: FormFields are tricky to get right and complex setups requiring these are probably beyond the scope of what this macro tries to do. the code below ensures that things don't break horribly if we run into one, but does not try too hard to get things pretty. #}
{{ field.label }} {%- for subfield in field %} {% if not bootstrap_is_hidden_field(subfield) -%} {{ render_field(subfield, form_type=form_type, horizontal_columns=horizontal_columns, button_map=button_map) }} {%- endif %} {%- endfor %}
{% else -%}
{%- if form_type == "inline" %} {{ field.label(class="sr-only")|safe }} {% if field.type == 'FileField' %} {% if field.errors %} {{ field(class="form-control-file is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control-file", **kwargs)|safe }} {% endif %} {% else %} {% if field.errors %} {{ field(class="form-control mb-2 mr-sm-2 mb-sm-0 is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control mb-2 mr-sm-2 mb-sm-0", **kwargs)|safe }} {% endif %} {% endif %} {% elif form_type == "horizontal" %} {{ field.label(class="form-control-label " + (" col-%s-%s" % horizontal_columns[0:2]))|safe }}
{% if field.type == 'FileField' %} {% if field.errors %} {{ field(class="form-control-file is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control-file", **kwargs)|safe }} {% endif %} {% else %} {% if field.errors %} {{ field(class="form-control is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control", **kwargs)|safe }} {% endif %} {% endif %}
{%- if field.errors %} {%- for error in field.errors %} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %}
{{ error }}
{% endcall %} {%- endfor %} {%- elif field.description -%} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %} {{ field.description|safe }} {% endcall %} {%- endif %} {%- else -%} {{ field.label(class="form-control-label")|safe }} {% if field.type == 'FileField' %} {% if field.errors %} {{ field(class="form-control-file is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control-file", **kwargs)|safe }} {% endif %} {% else %} {% if field.errors %} {{ field(class="form-control is-invalid", **kwargs)|safe }} {% else %} {{ field(class="form-control", **kwargs)|safe }} {% endif %} {% endif %} {%- if field.errors %} {%- for error in field.errors %}
{{ error }}
{%- endfor %} {%- elif field.description -%} {{ field.description|safe }} {%- endif %} {%- endif %}
{% endif %} {% endmacro %} {# valid form types are "basic", "inline" and "horizontal" #} {% macro render_form(form, action="", method="post", extra_classes=None, role="form", form_type="basic", horizontal_columns=('lg', 2, 10), enctype=None, button_map={}, id="", novalidate=False, render_kw={}) %} {#- action="" is what we want, from http://www.ietf.org/rfc/rfc2396.txt: 4.2. Same-document References A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request. -#} {#- if any file fields are inside the form and enctype is automatic, adjust if file fields are found. could really use the equalto test of jinja2 here, but latter is not available until 2.8 warning: the code below is guaranteed to make you cry =( #} {%- set _enctype = [] %} {%- if enctype is none -%} {%- for field in form %} {%- if field.type == 'FileField' %} {#- for loops come with a fairly watertight scope, so this list-hack is used to be able to set values outside of it #} {%- set _ = _enctype.append('multipart/form-data') -%} {%- endif %} {%- endfor %} {%- else %} {% set _ = _enctype.append(enctype) %} {%- endif %} {{ form.hidden_tag() }} {{ form_errors(form, hiddens='only') }} {%- for field in form %} {% if not bootstrap_is_hidden_field(field) -%} {{ render_field(field, form_type=form_type, horizontal_columns=horizontal_columns, button_map=button_map) }} {%- endif %} {%- endfor %} {%- endmacro %} {% macro render_form_row(fields, row_class='form-row', col_class_default='col', col_map={}) %} {# fields: an iterable of fields to render in a row. row_class: Class to apply to the div intended to represent the row, like 'form-row' or 'row'. col_class_default: Default col class to apply if nothing more specific is said about a field. col_class_dict: A dictionary which keys are fields.name and which values are the col class to apply to the field. Usages: Easiest use with defaults: {{ render_form_row([form.first_name, form.surname]) }} Custom col which should use class col-md-2, and the others the defaults: {{ render_form_row([form.title, form.first_name, form.surname], col_map={'title': 'col-md-2'}) }} Custom col which should use class col-md-2 and modified col class for the default of the other fields {{ render_form_row([form.title, form.first_name, form.surname], col_class_default='col-md-5', col_map={'title': 'col-md-2'}) }} #}
{% for field in fields %} {% if field.name in col_map %} {% set col_class = col_map[field.name] %} {% else %} {% set col_class = col_class_default %} {% endif %}
{{ render_field(field) }}
{% endfor %}
{% endmacro %}