This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
built_form.html
77 lines (64 loc) · 2.18 KB
/
built_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
{% load sekizai_tags %}
<h3 id="form_builder_{{ form.pk }}_title">{{ form.title }}</h3>
{% if form.intro %}
<p id="form_builder_{{ form.pk }}_intro">{{ form.intro }}</p>
{% endif %}
<p id ="form_builder_{{ form.pk }}_result"></p>
<form id="form_builder_{{ form.pk }}" action="{{ form.get_absolute_url }}" method="post" class="formsbuilder-form"
{% if form_for_form.is_multipart %}enctype="multipart/form-data"{% endif %}>
{% csrf_token %}
{% bootstrap_form form_for_form %}
{% buttons %}
<input type="submit" value="{{ form.button_text }}" class="btn btn-default">
{% endbuttons %}
</form>
{% addtoblock 'css' %}
<style>
/* preserve whitespace in django-forms-builder intro and
post-submission messages */
p#form_builder_{{ form.pk }}_intro,
p#form_builder_{{ form.pk }}_result {
white-space: pre-line;
}
</style>
{% endaddtoblock %}
{% addtoblock 'js' %}
<script>
$(document).ready(function() {
// handle form submission via ajax
$("#form_builder_{{ form.pk }}").on('submit', function(e) {
var url = "{{ form.get_absolute_url }}";
$.ajax({
type: "POST",
url: url,
data: $("#form_builder_{{ form.pk }}").serialize(),
success: function(data){
if($.isEmptyObject(data.errors)){
// no errors return by django-forms-builder
// hide all form elements and display success message
$('#form_builder_{{ form.pk }}_title').hide();
$('#form_builder_{{ form.pk }}_intro').hide();
$('#form_builder_{{ form.pk }}').hide();
$('#form_builder_{{ form.pk }}_result').html(data.message);
}
else {
// errors were returned, display them in a message
// at the top of the page
// create a list of error messages
var errors = '<ul>';
$.each(data.errors, function(k, v) {
errors += '<li>' + k + ': ' + v + '</li>';
});
errors += '</ul>'
$('form_builder_{{ form.pk }}_result').html('The following ' +
'errors were encountered during form submission: ' +
errors + '</div>');
}
}
});
// prevent default form POST
return false
});
});
</script>
{% endaddtoblock 'js' %}