blob: 0a33cb30cc52d3ae82d6b4d6c942857b11009b6e [file] [log] [blame]
Akronca9bd982016-12-06 16:59:57 +01001{# Renders field for bootstrap 3 standards.
2
3Params:
4field - WTForm field
5kwargs - pass any arguments you want in order to put them into the html attributes.
6There are few exceptions: for - for_, class - class_, class__ - class_
7
8Example usage:
9{{ macros.render_field(form.email, placeholder='Input email', type='email') }}
10#}
11{% macro render_field(field, label_visible=true) -%}
12
13<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
14 {% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
15 <label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
16 {% endif %}
17 {{ field(class_='form-control text-capitalized', **kwargs) }}
18 {% if field.errors %}
19 {% for e in field.errors %}
20 <p class="help-block">{{ e }}</p>
21 {% endfor %}
22 {% endif %}
23</div>
24{%- endmacro %}
25
26{# Renders checkbox fields since they are represented differently in bootstrap
27Params:
28field - WTForm field (there are no check, but you should put here only BooleanField.
29kwargs - pass any arguments you want in order to put them into the html attributes.
30There are few exceptions: for - for_, class - class_, class__ - class_
31
32Example usage:
33{{ macros.render_checkbox_field(form.remember_me) }}
34#}
35{% macro render_checkbox_field(field) -%}
36<div class="checkbox">
37 <label>
38 {{ field(type='checkbox', **kwargs) }} {{ field.label }}
39 </label>
40</div>
41{%- endmacro %}
42
43{# Renders radio field
44Params:
45field - WTForm field (there are no check, but you should put here only BooleanField.
46kwargs - pass any arguments you want in order to put them into the html attributes.
47There are few exceptions: for - for_, class - class_, class__ - class_
48
49Example usage:
50{{ macros.render_radio_field(form.answers) }}
51#}
52{% macro render_radio_field(field) -%}
53{% for value, label, _ in field.iter_choices() %}
54<div class="radio" style="margin:8pt auto;">
55 <label>
56 <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}">{{ label }}
57 </label>
58</div>
59{% endfor %}
60{%- endmacro %}
61
62{# Renders WTForm in bootstrap way. There are two ways to call function:
63- as macros: it will render all field forms using cycle to iterate over them
64- as call: it will insert form fields as you specify:
65e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
66class_='login-form') %}
67{{ macros.render_field(form.email, placeholder='Input email', type='email') }}
68{{ macros.render_field(form.password, placeholder='Input password', type='password') }}
69{{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
70{% endcall %}
71
72Params:
73form - WTForm class
74method - form method
75action_url - url where to submit this form
76action_text - text of submit button
77class_ - sets a class for form
78#}
79{% macro render_form(form, action_url='', method='POST', action_text='Submit', class_='', btn_class='btn btn-default') -%}
80
81<form method="{{ method }}" action="{{ action_url }}" role="form" class="{{ class_ }}">
82 {{ form.hidden_tag() if form.hidden_tag }}
83 {% if caller %}
84 {{ caller() }}
85 {% else %}
86 {% for f in form %}
87 {% if f.id != 'csrf_token' %}
88 {% if f.type == 'BooleanField' %}
89 {{ render_checkbox_field(f) }}
90 {% elif f.type == 'RadioField' %}
91 {{ render_radio_field(f) }}
92 {% else %}
93 {{ render_field(f) }}
94 {% endif %}
95 {% endif %}
96 {% endfor %}
97 {% endif %}
98 <button type="submit" class="{{ btn_class }}">{{ action_text }}</button>
99</form>
100{%- endmacro %}