-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_dynamic_fieldlist_better_remove.py
180 lines (155 loc) · 6.1 KB
/
form_dynamic_fieldlist_better_remove.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a minimal example of a Flask application that renders an actual HTML
document with a form containing a fieldlist and buttons to add input fields.
Run it using ::
python form_dynamic_fieldlist_better_remove.py
Open your browser and head to http://127.0.0.1:5000/ and look at the result.
"""
from flask import Flask, render_template_string, flash
from flask_wtf import Form
from wtforms import StringField, SubmitField, FieldList
from wtforms.validators import DataRequired
app = Flask(__name__)
app.secret_key = 'Secret Key'
class NameForm(Form):
names = FieldList(StringField('Name', validators=[DataRequired()]),
min_entries=1, max_entries=5)
submit = SubmitField('Submit')
template_string = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Form Dynamic Fieldlist</title>
<!-- Bootstrap CSS -->
<link href="{{ url_for('static', filename='css/bootstrap.css') }}" rel="stylesheet">
</head>
<body>
<div class="container">
{% with messages = get_flashed_messages() -%}
{%- for message in messages -%}
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{{ message|safe }}
</div>
{%- endfor -%}
{%- endwith %}
<h1>Simple Form</h1>
<form method="POST" action="/">
{{ form.hidden_tag() }}
<fieldset id="{{ form.names.id }}">
<label class="control-label">Names</label>
{% for name_field in form.names -%}
<div class="form-group{% if name_field.errors %} has-error{% endif %}">
<div class="input-group">
{{ name_field(class='form-control', placeholder=name_field.label.text)|safe }}
<span class="input-group-btn">
<button type="button" class="btn btn-default remove-name-field">
<span class="glyphicon glyphicon-remove"></span>
</button>
</span>
</div>
{% if name_field.errors -%}
<span class="help-block">
{% for error in name_field.errors -%}
{{ error }}
{%- endfor %}
</span>
{%- endif %}
</div>
{%- endfor %}
</fieldset>
<div class="form-group">
<button type="button" class="btn btn-default" id="add-name-field">
Add Field
</button>
</div>
<div class="form-group">
{{ form.submit(class="btn btn-primary")|safe }}
</div>
</form>
</div>
<!-- jQuery -->
<script src="{{ url_for('static', filename='js/jquery.js') }}"></script>
<!-- Bootstrap JS -->
<script src="{{ url_for('static', filename='js/bootstrap.js') }}"></script>
<script>
$(document).ready(function() {
var field_min_count = {{ form.names.min_entries|default(0, true) }};
var field_max_count = {{ form.names.max_entries|default('null', true) }};
var field_wrapper = $("fieldset#{{ form.names.id }}");
var field_template = '<div class="form-group"><div class="input-group"><input class="form-control" id="names-{n}" name="names-{n}" placeholder="Name" type="text" value=""><span class="input-group-btn"><button type="button" class="btn btn-default remove-name-field"><span class="glyphicon glyphicon-remove"></span></button></span></div></div>'
var add_field_button = $("button#add-name-field");
if (get_n_fields() <= field_min_count) {
deactivate_button(get_remove_buttons());
}
if (get_n_fields() >= field_max_count) {
deactivate_button(add_field_button);
}
function get_n_fields() {
return field_wrapper.children("div.form-group").length;
}
function deactivate_button(button) {
if (!button.attr("disabled")) {
button.attr("disabled", true);
}
}
function activate_button(button) {
if (button.attr("disabled")) {
button.removeAttr("disabled");
}
}
function get_remove_buttons() {
return field_wrapper.find("button.remove-name-field");
}
function add_field(event) {
event.preventDefault();
var n_fields = get_n_fields();
field_wrapper.append(field_template.replace(/{n}/g, n_fields));
activate_button(get_remove_buttons());
if (field_max_count != null && n_fields + 1 >= field_max_count) {
deactivate_button(add_field_button);
}
$("button.remove-name-field").off("click");
$("button.remove-name-field").click(remove_field);
};
function remove_field(event) {
event.preventDefault();
$(this).closest("div.form-group").remove();
if (field_min_count != null && get_n_fields() <= field_min_count) {
deactivate_button(get_remove_buttons());
}
activate_button(add_field_button);
renumber_fields();
};
function renumber_fields() {
var input_fields = field_wrapper.find("input[id^=names-][name^=names-]");
input_fields.attr("id", function(arr) {
return "names-" + arr;
});
input_fields.attr("name", function(arr) {
return "names-" + arr;
});
}
add_field_button.click(add_field);
get_remove_buttons().click(remove_field);
});
</script>
</body>
</html>
"""
@app.route('/', methods=('GET', 'POST'))
def index():
form = NameForm()
if form.validate_on_submit():
flash('You entered {}.'.format(
', '.join(['<strong>{}</strong>'.format(name.data) for name in form.names])))
return render_template_string(template_string, form=form)
if __name__ == '__main__':
app.run()