This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
remote_validation.js
75 lines (74 loc) · 3.05 KB
/
remote_validation.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
( function( $ ) {
var RemoteValidation = {
defaults: {
messageContainer : '#error_messages',
messageHtml : "<div id='error_explanation'><h2>{{NUM_ERRORS}} errors prohibited this post from being saved:</h2>{{ERRORS}}</div>",
noErrorClass: 'field',
errorClass : 'field_with_errors'
},
resetErrors: function(form, settings) {
$(settings.messageContainer).empty();
$("."+settings.errorClass, form).children().unwrap();
},
showErrors: function(form, settings, errors) {
var errorList ="";
var formFor = $(form).attr('id').replace(/new_|edit_/,"").replace(/_[0-9]+$/,"");
var numErrors = 0;
for (var col in errors) {
for (var i in errors[col]) {
errorList += "<li> "+col+" "+errors[col][i]+"</li>";
$("*[name='"+formFor+"["+col+"]']", form).wrap($("<div/>").addClass(settings.errorClass));
$("label[for='"+formFor+"_"+col+"']", form).wrap($("<div/>").addClass(settings.errorClass));
numErrors++;
}
}
var errorHtml = settings.messageHtml.replace("{{NUM_ERRORS}}", numErrors);
errorHtml = errorHtml.replace("{{ERRORS}}","<ul>"+errorList+"</ul>");
$(settings.messageContainer).append(errorHtml);
}
};
$.fn.remoteValidation = function(options) {
var settings = $.extend({}, RemoteValidation.defaults, options );
return this.each( function() {
var $this = this; //$(this), may change inside function, so assign it to $this.
$(this).submit(function() {
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method') || 'GET',
data: $(this).serializeArray(),
dataType: $(this).data('type') || 'json',
crossDomain: null,
beforeSend: function(xhr) {
RemoteValidation.resetErrors($this, settings);
if (typeof settings.beforeSend == 'function') {
settings.beforeSend.apply($this, [xhr]);
}
},
success: function(data, status, xhr) {
if (data.errors) { // 200 response, but errors are specified
RemoteValidation.showErrors($this, settings, data.errors);
} else if (data.location) { // 200 response, but redirect required
window.location.href = data.location;
}
if (typeof settings.success == 'function') {
settings.success.apply($this, [data, status, xhr]);
}
},
error: function(xhr, status, error) {
var data = JSON.parse(xhr.responseText);
RemoteValidation.showErrors($this, settings, data);
if (typeof settings.error == 'function') {
settings.error.apply($this, [xhr, status, error]);
}
},
complete: function(xhr, status) {
if (typeof settings.complete == 'function') {
settings.complete.apply($this, [xhr, status]);
}
}
});
return false;
}); // $(this).submit
}) // return this.each
} // $.fn.remoteValidation
})(jQuery);