-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_forms.js
135 lines (117 loc) · 4.52 KB
/
ajax_forms.js
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
// JavaScript Document
jQuery.fn.ajax_form = function() {
$(this).each(function() {
$(this).on('submit',ajax_forms.process_form)
.on('change keyup paste','input,textarea,select',function() {
ajax_forms.hideError($(this));
});
});
return this;
};
$(function() {
$('form[data-submit="ajax"]').ajax_form();
});
var ajax_forms = {
process_form: function(event) {
event.preventDefault();
var form = $(this);
var data = form.serialize();
var url = form.attr('action');
data.responseType = 'json';
console.log("submitting form");
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: data,
success: function(data) {
console.log("got response");
if ( data.ok ) {
if ( ! data.redirect ) {
data.redirect = window.location.href;
}
window.location.href = data.redirect;
} else {
let messages = [];
if ( data.message ) {
messages.push(data.message);
}
let errors = [];
if ( typeof data.errors === 'object' ) {
Object.keys(data.errors).forEach(function(key,index) {
errors.push({field:key,msg:data.errors[key]});
});
} else {
errors = data.errors;
}
if ( errors.length ) {
// display errors
$.each(errors,function(index,error) {
// get the input element
var element = $('[name='+error.field+']',form).first();
if ( element.length ) {
ajax_forms.showError(element,error.msg);
} else {
messages.push(error.field+": "+error.msg);
}
});
// set focus on first error
$('#'+errors[0].field).focus();
}
// Display popup if necessay
if ( messages.length ) {
if ( typeof bootbox !== 'undefined' ) {
bootbox.alert({title: '<i class="fa fa-exclamation-circle text-danger"></i> Error',
message: messages.join('<br />')
});
} else if ( typeof swal !== 'undefined' ) {
swal(messages.join("\n"), {
icon: "error"
});
} else {
alert(messages.join("\n"));
}
}
}
},
error: function(xhr,status) {
alert(xhr.responseText);
}
}); /* $.ajax */
}, /* process_form */
hideError: function(element) {
element.removeClass('is-invalid');
let name = element.attr('name');
let errorElement = ajax_forms.errorElements[name];
if ( errorElement !== undefined ) {
errorElement.hide();
}
},
showError: function(element,error) {
// set error class on element
element.addClass('is-invalid');
// get the error element
let name = element.attr('name');
let errorElement = ajax_forms.errorElements[name];
if ( errorElement === undefined ) {
// create new error element
errorElement = $('<span></span>')
.addClass("invalid-feedback")
.text(error);
// insert error element
if (element.prop('type') === 'radio') {
errorElement.appendTo(element.closest('div'));
} else if (element.parent('.input-group').length || element.prop('type') === 'checkbox' ) {
errorElement.insertAfter(element.parent());
} else {
errorElement.insertAfter(element);
}
// save error element for later
ajax_forms.errorElements[name] = errorElement;
} else {
// use existing error element
errorElement.text(error).show();
}
},
errorElements: {}
}; /* ajax_forms */