-
Notifications
You must be signed in to change notification settings - Fork 3
/
horizontal_b4.html
341 lines (307 loc) · 12.6 KB
/
horizontal_b4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
{%- if _ is not defined -%}
{% macro _(message) -%}
{{ message }}
{%- endmacro %}
{%- endif -%}
{# Renders field for bootstrap 4 standards.
Params:
field - WTForm field
kwargs - pass any arguments you want in order to put them into the html attributes.
There are few exceptions: for - for_, class - class_, class__ - class_
Example usage:
{{ horz_form.field(form.email, placeholder='Input email', type='email') }}
Note:
A change was made to the below macros to support adding tab indexes to form
elements. A tab index can be added explicitly as a html attribute when creating a
field but we wanted to add the tab indexes when a wtform is created using the
form macro, fields macro or the sections macro.
Due to the fact that some of these macros can be used multiple times in a template,
we needed a way to specify the start tab index for each invocation of the macro. We
do that by passing in the start_tab_index parameter to the form, fields and section
macros.
This change could possibly cause tab indexes to be incorrect for existing forms
if some form fields are being rendered explicity inside a form macro. The tab indexes
would be incorrect because by default, the start_tab_index in the form macro is set to
1 and the same index is then used to set the tab index on the form submit buttons. To fix
the tab indexes, the parameter start_tab_index in the form macro will need to be
set so to account for all the fields which are being explicitly rendered.
#}
{% macro div_form_group(field) -%}
<div class="form-group row {{ 'required' if field.flags.required }} {{ kwargs.get('class_', '') }}">
{{ caller(**kwargs) }}
</div>
{%- endmacro %}
{% macro description(field) -%}
<small class="form-text text-muted description">
{% if field.description is callable %}
{{ field.description()|safe }}
{% else %}
{{ field.description|safe }}
{% endif %}
</small>
{%- endmacro %}
{% macro field_errors(field) -%}
{# You will need javascript similar to below for validations to work properly.
Alternatively, remove .was-validated from the form element to disable native client side validations by default.
var forms = document.getElementsByClassName('needs-validation');
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
var fields = document.getElementsByClassName('is-invalid');
Array.prototype.filter.call(fields, function(field) {
field.setCustomValidity(false);
});
#}
<div class="invalid-feedback">
{% if field.errors %}
{% for e in field.errors %}
<p>{{ e }}</p>
{% endfor %}
{% elif field.flags.required %}
<p>This field is required.</p>
{% endif %}
</div>
{% endmacro %}
{% macro field(field, label_visible=true) -%}
{% call div_form_group(field, **kwargs) %}
{% if (field.type != 'HiddenField' and field.type != 'CSRFTokenField') and label_visible %}
{{ field.label(class_='col-form-label col-3') }}
{% endif %}
<div class="col-9">
<div class="labeled-group">
{{ field_widget(field, **kwargs) }}
{% if field.description %}
{{ description(field) }}
{% endif %}
</div>
</div>
{% endcall %}
{%- endmacro %}
{% macro custom_css() %}
<!-- included from keg-elements custom-css macro -->
<style>
.multi-checkbox > * {
flex: 1 1 48%;
margin: 0 1%;
}
.multi-checkbox-controls {
padding: 0.5em 1em;
}
</style>
{% endmacro %}
{% macro custom_js() %}
<!-- included from keg-elements custom-js macro -->
<script>
document.querySelectorAll('[multi-checkbox-data^="select-"]').forEach(item => {
const target_value = item.attributes['multi-checkbox-data'].value.indexOf('select-all') !== -1;
item.addEventListener('click', event => {
event.target.parentElement.parentElement.querySelectorAll('.custom-checkbox input').forEach(checkbox => {
checkbox.checked = target_value;
});
event.preventDefault();
})
})
</script>
{{ datetime_helper() }}
{% endmacro %}
{% macro multi_checkbox(field) %}
<div class="multi-checkbox-controls">
<button multi-checkbox-data="select-all">Select All</button>
<button multi-checkbox-data="select-none">Select None</button>
</div>
<div id="{{ field.id }}" class="d-flex flex-row flex-wrap multi-checkbox">
{% for choice_id, choice in field.choices %}
<div class="custom-control custom-checkbox">
<input type="checkbox" name="{{ field.name }}"
{{ 'checked="checked"' if field.data and choice_id in field.data else "" }}
class="custom-control-input" value="{{ choice_id }}" id="{{ field.id }}{{choice_id}}">
<label class="col-form-label custom-control-label" for="{{ field.id }}{{ choice_id }}">{{choice}}</label>
</div>
{% endfor %}
</div>
{% endmacro %}
{% macro field_widget(field) %}
{% if field.type == "MultiCheckboxField" %}
{{ multi_checkbox(field) }}
{% else %}
{% if field.flags.disabled %}{% set _dummy = kwargs.update({'disabled': field.flags.disabled}) %}{% endif %}
{% if field.flags.readonly %}{% set _dummy = kwargs.update({'readonly': field.flags.readonly}) %}{% endif %}
{{ field(class_='form-control is-invalid' if field.errors else 'form-control', **kwargs) }}
{% endif %}
{{ field_errors(field) }}
{% endmacro %}
{# Renders checkbox fields since they are represented differently in bootstrap
Params:
field - WTForm field (there are no check, but you should put here only BooleanField.
kwargs - pass any arguments you want in order to put them into the html attributes.
There are few exceptions: for - for_, class - class_, class__ - class_
Example usage:
{{ horiz_form.checkbox_field(form.remember_me) }}
#}
{% macro checkbox_field(field) -%}
{% call div_form_group(field, **kwargs) %}
<div class="col-sm-9 offset-sm-3">
<div class="pt-2 unlabeled-group checkbox form-check custom-control custom-checkbox">
{{ field(type='checkbox', class_='form-check-input custom-control-input' + (' is-invalid' if field.errors else ''), **kwargs) }}
{# pt-0 is to align the label with the checkbox by removing the padding #}
<label class="pt-0 col-form-label form-check-label custom-control-label" for="{{ field.id }}">
{{ field.label }}
</label>
{{ field_errors(field) }}
</div>
{% if field.description %}
{{ description(field) }}
{% endif %}
</div>
{% endcall %}
{%- endmacro %}
{# Renders radio field
Params:
field - WTForm field (must have an `iter_choices` method)
kwargs - pass any arguments you want in order to put them into the html attributes.
There are few exceptions: for - for_, class - class_, class__ - class_
Example usage:
{{ horiz_form.radio_field(form.answers) }}
#}
{% macro radio_field(field, label_visible=true, tabIndex=1) -%}
<fieldset class="form-group row {{ 'required' if field.flags.required }} {{ kwargs.get('class_', '') }}">
{% if label_visible %}
{{ field.label(class_='col-form-label col-3') }}
{% endif %}
<div class="col-9">
{% for value, label, checked in field.iter_choices() %}
<div class="radio form-check">
<input type="radio"
class=""form-check-input"
name="{{ field.id }}"
id="{{ field.id }}"
value="{{ value }}"
{{ 'checked' if checked }}
{{ 'disabled' if field.flags.disabled or (field.flags.readonly and not checked) }}
tabIndex="{{tabIndex}}">
<label class="form-check-label">
{{ label }}
</label>
</div>
{% endfor %}
{{ field_errors(field) }}
</div>
{% if field.description %}
{{ description(field) }}
{% endif %}
</fieldset>
{%- endmacro %}
{% macro submit_group(action_text='Submit', btn_class='btn btn-primary', cancel_url='', tab_index=1) -%}
<div class="form-group row col-9 offset-sm-3">
<div class="unlabeled-group">
<button type="submit" class="{{ btn_class }}" tabIndex="{{tab_index}}">{{ action_text }} </button>
{% if cancel_url %}
<a href="{{cancel_url}}" class="cancel" tabIndex="{{tab_index+1}}">Cancel</a>
{% endif %}
</div>
</div>
{%- endmacro %}
{% macro render_field(f) -%}
{% if f is none %}
{# Do nothing b/c the field is None #}
{% elif f.type == 'BooleanField' %}
{{ checkbox_field(f, **kwargs) }}
{% elif f.type == 'RadioField' %}
{{ radio_field(f, **kwargs) }}
{% else %}
{{ field(f, **kwargs) }}
{% endif %}
{%- endmacro %}
{% macro form_errors(form) -%}
{% if form.form_errors %}
<div class="text-danger">
{% for e in form.form_errors %}
<p>{{ e }}</p>
{% endfor %}
</div>
{% endif %}
{% endmacro %}
{# Renders WTForm in bootstrap way. There are two ways to call function:
- as macros: it will render all field forms using cycle to iterate over them
- as call: it will insert form fields as you specify:
e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
class_='login-form') %}
{{ macros.render_field(form.email, placeholder='Input email', type='email') }}
{{ macros.render_field(form.password, placeholder='Input password', type='password') }}
{{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
{% endcall %}
Params:
form - WTForm class
action_url - url where to submit this form
action_text - text of submit button
class_ - sets a class for form
#}
{% macro form(
form,
field_names=None,
action_url='',
action_text='Submit',
class_='form-horizontal',
btn_class='btn btn-primary',
cancel_url='',
form_upload=false,
dirty_check=false,
start_tab_index=1,
form_id=None,
form_name=None
) -%}
<form method="POST"
action="{{ action_url }}"
role="form"
class="{{ class_ }} {{ 'was-validated needs-validation' if form.errors else 'needs-validation' }}"
{% if form_id %}id="{{ form_id }}"{% endif %}
{% if form_name %}name="{{ form_name }}"{% endif %}
{% if form_upload %}enctype="multipart/form-data"{% endif %}
{% if dirty_check %}data-dirty-check="on"{% endif %}
novalidate
>
{{ form.hidden_tag() if form.hidden_tag }}
{% set tab_index=[start_tab_index|int] %}
{{ form_errors(form) }}
{% if caller %}
{{ caller() }}
{% elif field_names %}
{{ fields(form, field_names) }}
{% else %}
{% for f in form %}
{% if not f.widget.input_type == 'hidden' %}
{# Hidden fields are rendered by the `hidden_tag()` call above #}
{{ render_field(f, tabIndex=tab_index.pop()) }}
{% if tab_index.append(start_tab_index|int+loop.index) %} {% endif %}
{% endif %}
{% endfor %}
{% endif %}
{{ submit_group(action_text=action_text, btn_class=btn_class, cancel_url=cancel_url, tab_index=tab_index[0]) }}
</form>
{%- endmacro %}
{% macro fields(form, field_names, start_tab_index=1) -%}
{% set tab_index = start_tab_index|int %}
{% for field_name in field_names %}
{% set tab_index = tab_index + loop.index0 %}
{{ render_field(form[field_name], tabIndex=tab_index) }}
{% endfor %}
{%- endmacro %}
{% macro section(heading, form, field_names, start_tab_index=1) -%}
<h2>{{heading}}</h2>
{% if caller %}
{{ caller() }}
{% else %}
{{ fields(form, field_names, start_tab_index) }}
{% endif %}
{%- endmacro %}
{% macro datetime_helper() -%}
<script type="text/javascript">
{% include "keg_elements/forms/datetime_helper.js" %}
</script>
{%- endmacro %}