blob: 2d1c62a5bed4236fec4d39c50c84272c7873b730 [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 {% if field.errors %}
59 {% for e in field.errors %}
60 <p class="help-block">{{ e }}</p>
61 {% endfor %}
62 {% endif %}
63</div>
64{% endfor %}
65{%- endmacro %}
66
67
68{# Renders WTForm in bootstrap way. There are two ways to call function:
69- as macros: it will render all field forms using cycle to iterate over them
70- as call: it will insert form fields as you specify:
71e.g. {% call macros.render_form(form, action_url=url_for('.login_view'), action_text='Login',
72class_='login-form') %}
73{{ macros.render_field(form.email, placeholder='Input email', type='email') }}
74{{ macros.render_field(form.password, placeholder='Input password', type='password') }}
75{{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
76{% endcall %}
77
78Params:
79form - WTForm class
80method - form method
81action_url - url where to submit this form
82action_text - text of submit button
83class_ - sets a class for form
84#}
85{% macro render_form(form, action_url='', method='POST', action_text='Submit', class_='', disabled=False,
86btn_class='btn btn-default') -%}
87
88<form method="{{ method }}" action="{{ action_url }}" role="form" class="{{ class_ }}">
89 {{ form.hidden_tag() if form.hidden_tag }}
90 {% if caller %}
91 {{ caller() }}
92 {% else %}
93 {% for f in form %}
94 {% if f.id != 'csrf_token' %}
95 {% if f.type == 'BooleanField' %}
96 {{ render_checkbox_field(f) }}
97 {% elif f.type == 'RadioField' %}
98 {{ render_radio_field(f) }}
99 {% else %}
100 {{ render_field(f) }}
101 {% endif %}
102 {% endif %}
103 {% endfor %}
104 {% endif %}
105 <button type="submit" {% if disabled %} disabled {% endif %} class="{{ btn_class }}">{{ action_text }}</button>
106</form>
107{%- endmacro %}