-
Notifications
You must be signed in to change notification settings - Fork 2k
/
form.html
252 lines (210 loc) · 10.4 KB
/
form.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
{#
Creates all the markup required for an input element. Handles matching labels to
inputs, error messages and other useful elements.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input.
placeholder - Some placeholder text.
type - The type of input eg. email, url, date (default: text).
error - A list of error strings for the field.
classes - An array of classes to apply to the control-group.
Examples:
{% import 'macros/form.html' as form %}
{{ form.input('title', label=_('Title'), value=data.title, error=errors.title) }}
#}
{% macro input(name, id='', label='', value='', placeholder='', type='text', error="", classes=[], attrs={}) %}
{% call block(id or name, label or name, error, classes) %}
<input id="{{ id or name }}" type="{{ type }}" name="{{ name }}" value="{{ value | e }}" placeholder="{{ placeholder }}"{{ attributes(attrs) }} />
{% endcall %}
{% endmacro %}
{#
Builds a single checkbox input.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input.
checked - If true the checkbox will be checked
error - An error string for the field.
classes - An array of classes to apply to the control-group.
Example:
{% import 'macros/form.html' as form %}
{{ form.checkbox('remember', checked=true) }}
#}
{% macro checkbox(name, id='', label='', value='', checked=false, placeholder='', error="", classes=[], attrs={}) %}
<div class="control-group{{ " " ~ classes | join(" ") }}{% if error %} error{% endif %}">
<div class="controls">
<label class="checkbox" for="{{ id or name }}">
<input id="{{ id or name }}" type="checkbox" name="{{ name }}" value="{{ value | e }}" {{ "checked " if checked }}{{ attributes(attrs) }} />
{{ label or name }}
{% if error %}<strong class="error-inline">{{ error|join(', ') }}</strong>{% endif %}
</label>
</div>
</div>
{% endmacro %}
{#
Creates all the markup required for an select element. Handles matching labels to
inputs and error messages.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
options - A dict of value/label pairs to be used as <options>.
selected - The value of the selected <option>.
error - A list of error strings for the field.
classes - An array of classes to apply to the control-group.
Examples:
{% import 'macros/form.html' as form %}
{{ form.select('year', label=_('Year'), options={2010: 2010, 2011: 2011}, selected=2011, error=errors.year) }}
#}
{% macro select(name, id='', label='', options='', selected='', error='', classes=[], attrs={}) %}
{% set classes = (classes|list) %}
{% do classes.append('control-select') %}
{% call block(id or name, label or name, error, classes) %}
<select id="{{ id or name }}" name="{{ name }}"{{ attributes(attrs) }}>
{% for value, text in options.items() %}
<option value="{{ value }}"{% if value == selected %} selected{% endif %}>{{ text }}</option>
{% endfor %}
</select>
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for a Markdown textarea element. Handles
matching labels to inputs, selected item and error messages.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input.
placeholder - Some placeholder text.
error - A list of error strings for the field.
classes - An array of classes to apply to the control-group.
Examples:
{% import 'macros/form.html' as form %}
{{ form.markdown('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc) }}
#}
{% macro markdown(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={}) %}
{% set classes = (classes|list) %}
{% do classes.append('control-full') %}
{% call block(id or name, label or name, error, classes, control_classes=["editor"]) %}
<textarea id="{{ id or name }}" name="{{ name }}" cols="20" rows="5" placeholder="{{ placeholder }}"{{ attributes(attrs) }}>{{ value | e }}</textarea>
<span class="info-block">{% trans %}You can use <a href="http://daringfireball.net/projects/markdown/syntax" target="_blank">Markdown formatting</a> here{% endtrans %}</span>
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for an input element with a prefixed segment.
These are useful for showing url slugs and other fields where the input
information forms only part of the saved data.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
prepend - The text that will be prepended before the input.
value - The value of the input.
which will use the name key as the value.
placeholder - Some placeholder text.
error - A list of error strings for the field.
classes - An array of classes to apply to the control-group.
Examples:
{% import 'macros/form.html' as form %}
{{ form.prepend('slug', id='field-slug', prepend='/dataset/', label=_('Slug'), value=data.slug, error=errors.slug) }}
#}
{% macro prepend(name, id='', label='', prepend='', value='', placeholder='', type='text', error="", classes=[], attrs={}) %}
{# We manually append the error here as it needs to be inside the .input-prepend block #}
{% set classes = (classes|list) %}
{% do classes.append('error') if error %}
{% call block(id or name, label or name, error='', classes=classes) %}
<div class="input-prepend">
{% if prepend %}<span class="add-on">{{ prepend }}</span>{%- endif -%}
<input id="{{ id or name }}" type="{{ type }}" name="{{ name }}" value="{{ value | e }}" placeholder="{{ placeholder }}"{{ attributes(attrs) }} />
{% if error %}<span class="error-block">{{ error|join(', ') }}</span>{% endif %}
</div>
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for an custom key/value input. These are usually
used to let the user provide custom meta data. Each "field" has three inputs
one for the key, one for the value and a checkbox to remove it. So the arguments
for this macro are nearly all tuples containing values for the
(key, value, delete) fields respectively.
name - A tuple of names for the three fields.
id - An id string to be used for each input.
label - The human readable label for the main label.
values - A tuple of values for the (key, value, delete) fields. If delete
is truthy the checkbox will be checked.
placeholder - A tuple of placeholder text for the (key, value) fields.
error - A list of error strings for the field.
classes - An array of classes to apply to the control-group.
Examples:
{% import 'macros/form.html' as form %}
{{ form.custom(
names=('custom_key', 'custom_value', 'custom_deleted'),
id='field-custom',
label=_('Custom Field'),
values=(extra.key, extra.value, extra.deleted),
error=''
) }}
#}
{% macro custom(names=(), id="", label="", values=(), placeholders=(), error="", classes=[], attrs={}) %}
{% set classes = (classes|list) %}
{% set label_id = (id or names[0]) ~ "-key" %}
{% do classes.append('control-custom') %}
{% call block(label_id, label or name, error, classes, control_classes=["editor"]) %}
<div class="input-prepend" {{ attributes(attrs) }}>
<label for="{{ label_id }}" class="add-on">Key</label><input id="{{ id or names[0] }}-key" name="{{ names[0] }}" value="{{ values[0] | e }}" placeholder="{{ placeholders[0] }}" />
<label for="{{ id or names[1] }}-value" class="add-on">Value</label><input id="{{ id or names[1] }}-value" name="{{ names[1] }}" value="{{ values[1] | e }}" placeholder="{{ placeholders[1] }}" />
{% if values[0] or values[1] or error %}
<label class="checkbox" for="{{ id or names[2] }}-remove">
<input type="checkbox" id="{{ id or names[2] }}-remove" name="{{ names[2] }}"{% if values[2] %} checked{% endif %} /> {{ _('Remove') }}
</label>
{% endif %}
</div>
{% endcall %}
{% endmacro %}
{#
A generic block for providing the default markup for CKAN form elements.
It is expected to be called using a {% call %} block, the contents of which
will be inserted into the .controls element.
for - The id for the input that the label should match.
label - A human readable label.
error - A list of error strings for the field.
classes - An array of custom classes for the outer element.
control_classes - An array of custom classes for the .control wrapper.
Example:
{% import 'macros/form.html' as form %}
{% call form.block("field", "My Field") %}
<input id="field" type="text" name="{{ name }}" value="{{ value | e }}" />
{% endcall %}
#}
{% macro block(for, label="", error="", classes=[], control_classes=[]) %}
<div class="control-group{{ " error" if error }}{{ " " ~ classes | join(' ') }}">
<label class="control-label" for="{{ for }}">{{ label or _('Custom') }}</label>
<div class="controls{{ " " ~ control_classes | join(' ') }}">
{{ caller() }}
{% if error %}<span class="error-block">{{ error|join(', ') }}</span>{% endif %}
</div>
</div>
{% endmacro %}
{#
Builds a list of errors for the current form.
errors - A dict of field/message pairs.
type - The alert-* class that should be applied (default: "error")
classes - A list of classes to apply to the wrapper (default: [])
Example:
{% import 'macros/form.html' as form %}
{{ form.errors(error_summary, type="warning") }}
#}
{% macro errors(errors={}, type="error", classes=[]) %}
{% if errors %}
<div class="error-explanation alert alert-{{ type }}{{ " " ~ classes | join(' ') }}">
<p>{{ _('The form contains invalid entries:') }}</p>
<ul>
{% for key, error in errors.items() %}
<li data-field-label="{{ key }}">{{ key }}: {{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endmacro %}
{%- macro attributes(attrs={}) -%}
{%- for key, value in attrs.items() -%}
{{ key }}="{{ value }}"
{%- endfor -%}
{%- endmacro -%}