-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.validate.unobtrusive.bootstrap.js
115 lines (99 loc) · 2.67 KB
/
jquery.validate.unobtrusive.bootstrap.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
/*!
* jQuery Validate Unobtrusive Bootstrap 1.2.3
*
* https://github.com/sandrocaseiro/jquery.validate.unobtrusive.bootstrap
*
* Copyright 2014 Sandro Caseiro
* Released under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
(function($)
{
function escapeAttributeValue(value)
{
// As mentioned on http://api.jquery.com/category/selectors/
return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
}
function addErrorClass(element)
{
var group = element.closest('.form-group');
if (group && group.length > 0)
{
group.addClass('has-error').removeClass('has-success');
}
}
function addSuccessClass(element)
{
var group = element.closest('.form-group');
if (group && group.length > 0)
{
group.addClass('has-success').removeClass('has-error');
}
}
function onError(formElement, errorPlacementBase, error, inputElement)
{
errorPlacementBase(error, inputElement);
if ($(inputElement).hasClass('input-validation-error'))
{
addErrorClass(inputElement)
}
}
function onSuccess(successBase, error)
{
var container = error.data("unobtrusiveContainer");
successBase(error);
if (container)
{
addSuccessClass(container);
}
}
$.fn.validateBootstrap = function(refresh)
{
return this.each(function()
{
var $this = $(this);
if (refresh)
{
$this.removeData('validator');
$this.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse($this);
}
var validator = $this.data('validator');
if (validator)
{
validator.settings.errorClass += ' text-danger';
var errorPlacementBase = validator.settings.errorPlacement;
var successBase = validator.settings.success;
validator.settings.errorPlacement = function(error, inputElement)
{
onError($this, errorPlacementBase, error, inputElement);
};
validator.settings.success = function(error)
{
onSuccess(successBase, error);
}
$this.find('.input-validation-error').each(function()
{
var errorElement = $this.find("[data-valmsg-for='" + escapeAttributeValue($(this)[0].name) + "']");
var newElement = $(document.createElement(validator.settings.errorElement))
.addClass('text-danger')
.attr('for', escapeAttributeValue($(this)[0].name))
.text(errorElement.text());
onError($this, errorPlacementBase, newElement, $(this));
});
}
// if validation isn't enabled, but the form has the validation error message element, add error class to container
else
{
$this.find('.input-validation-error').each(function()
{
addErrorClass($(this));
});
}
});
};
$(function()
{
$('form').validateBootstrap();
});
}(jQuery));