Skip to content

Commit 2c64d04

Browse files
committed
forms
1 parent 7f80c6f commit 2c64d04

File tree

11 files changed

+281
-82
lines changed

11 files changed

+281
-82
lines changed

assets/static/js/app.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ $('#confirmAction').on('show.bs.modal', function (event) {
77
$(this).find('.modal-content form input[type=password]').val('');
88
});
99

10-
1110
$('#confirmAction').find('.modal-content form').on('submit', function(event) {
1211

1312
$.ajax({
@@ -36,3 +35,68 @@ $('#confirmAction').find('.modal-content form').on('submit', function(event) {
3635

3736
return false
3837
});
38+
39+
$('#changePassword').on('hide.bs.modal', function (event) {
40+
$(this).find('.modal-dialog form').trigger("reset");
41+
});
42+
43+
$('#changePassword form').bootstrap3Validate(function(e, data) {
44+
45+
e.preventDefault();
46+
47+
$.ajax({
48+
method : 'POST',
49+
url : '/password/change/',
50+
data : data,
51+
dataType : 'json',
52+
success : function(data, event) {
53+
location.reload(true);
54+
},
55+
error: function(request, status, error) {
56+
57+
var data = JSON.parse(request.responseText);
58+
59+
if (data.error !== undefined) {
60+
61+
alert(data.error);
62+
63+
return;
64+
}
65+
66+
alert(error);
67+
}
68+
});
69+
70+
return false
71+
});
72+
73+
$('#addProject form').bootstrap3Validate(function(e, data) {
74+
75+
e.preventDefault();
76+
77+
$.ajax({
78+
method : 'POST',
79+
url : '/project/add/',
80+
data : data,
81+
dataType : 'json',
82+
success : function(data, event) {
83+
location.reload(true);
84+
},
85+
error: function(request, status, error) {
86+
87+
var data = JSON.parse(request.responseText);
88+
89+
if (data.error !== undefined) {
90+
91+
alert(data.error);
92+
93+
return;
94+
}
95+
96+
alert(error);
97+
}
98+
});
99+
100+
return false
101+
});
102+

assets/static/js/validator.min.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Bootstrap.validator plugin for JQuery and Bootstrap 3.
3+
* https://github.com/wpic/bootstrap.validator.js
4+
* By Hamed Abdollahpour 2014 - WPIC Co
5+
*/
6+
(function ( $ ) {
7+
8+
$.fn.validate = function( options ) {
9+
10+
var settings = $.extend({
11+
init: function() {}, /* init even */
12+
success: function() {}, /* sucess even */
13+
fail: function(invalids) {} /* Fail even */
14+
/* join: ' ' */ /* Join array field data with it - If it be empty it will still remain array */
15+
}, options );
16+
17+
var validators = {
18+
'name+family': /^((?![0-9]).+\s.+)/g,
19+
'name': /^((?![0-9]).+)/g,
20+
'family': /^((?![0-9]).+)/g,
21+
'number': /^[0-9]+/g,
22+
'url': /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/,
23+
'date': /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/,
24+
'email': /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
25+
'tel': /^[0-9\-\+]{3,25}$/
26+
};
27+
28+
return this.on('submit', function(e) {
29+
var form = this;
30+
var invalids = [];
31+
32+
settings.init.call(form);
33+
34+
var data = {};
35+
$('input,textarea,select', this).filter(":visible,[type='hidden']").each(function() {
36+
var i = $(this);
37+
if(!i.parent().is(':visible')) {
38+
return;
39+
}
40+
var name = i.attr('name');
41+
if(typeof(name) != 'undefined' && (i.is(":not([type='checkbox'],[type='radio'])") || i.is(":checked"))) {
42+
var value = i.val();
43+
44+
if(typeof(data[name]) != 'undefined') {
45+
data[name] = [].concat(data[name]).concat(value);
46+
} else {
47+
data[name] = value;
48+
}
49+
}
50+
});
51+
52+
/* join array if it's required */
53+
if (typeof(settings.join) != 'undefined') {
54+
for(var i in data) {
55+
if (typeof(data[i]) == "function") {
56+
data[i] = data[i].join(settings.join);
57+
}
58+
}
59+
}
60+
61+
/** [data-require] is deprecated use requried instead **/
62+
$("[data-regex],[data-require],[data-required],[required],[data-equals]", form).filter(':visible').each(function() {
63+
var self = $(this);
64+
65+
var regex = self.attr('data-regex');
66+
var required = self.is('[required]') || self.is('[data-required]') /* the rest deprecated */ || self.is('[data-require]');
67+
var equals = self.attr('data-equals');
68+
var value = self.val();
69+
70+
if(self.is("[type='checkbox']") && !self.is(":checked")) value='';
71+
72+
// replace value with total value
73+
var name = self.attr('name');
74+
if(typeof(name) != 'undefined') {
75+
value = data[name];
76+
}
77+
78+
if(typeof(equals) != 'undefined' && typeof(data[equals]) != 'undefined') {
79+
if(value != data[equals]) {
80+
invalids.push(this);
81+
invalids.push($("[name='" + equals + "']")[0]);
82+
}
83+
}
84+
85+
/** array is always valid, because for array inputs we just check required **/
86+
if(!Array.isArray(value)) {
87+
if(value && value.length > 0) {
88+
// try to use tag name for regex
89+
if (typeof(regex) == 'undefined') {
90+
var type = $(this).attr('type');
91+
if (typeof(type) != 'undefined' && typeof(validators[type.toLowerCase()]) != 'undefined') {
92+
regex = type.toLowerCase();
93+
}
94+
}
95+
96+
if (regex) {
97+
var r;
98+
if (typeof(validators[regex]) != 'undefined') {
99+
r = validators[regex]
100+
}
101+
// Use regex itself
102+
else {
103+
r = new RegExp(regex);
104+
}
105+
106+
if(!r.test(value)) {
107+
invalids.push(this);
108+
}
109+
}
110+
}
111+
else if(required) {
112+
invalids.push(this);
113+
}
114+
}
115+
});
116+
117+
if(invalids.length > 0) {
118+
e.preventDefault();
119+
settings.fail.call(form, e, invalids, data);
120+
} else {
121+
settings.success.call(form, e, data);
122+
}
123+
})
124+
// Disable default HTML5 validation
125+
.attr('novalidate', '');
126+
127+
};
128+
129+
$.fn.bootstrap3Validate = function(success, data) {
130+
return this.validate({
131+
'init': function() {
132+
$('.has-error', this).removeClass('has-error').find('input,textarea').tooltip('destroy');
133+
$('.alert').hide();
134+
$("[role='tooltip']", this).tooltip('destroy');
135+
},
136+
'success': function(e, data) {
137+
if (typeof(success) === 'function') {
138+
success.call(this, e, data);
139+
}
140+
},
141+
'fail': function(e, invalids) {
142+
var form = this;
143+
144+
$(invalids).closest('.form-group').addClass('has-error').find('input,select,textarea').each(function(i) {
145+
var target = $(this);
146+
var text = target.attr('data-title');
147+
if(!text) {
148+
text = target.attr('placeholder');
149+
}
150+
151+
if(text) {
152+
if(!target.is("[type='checkbox']")) {
153+
target.tooltip({'trigger':'focus', placement: 'top', title: text});
154+
}
155+
156+
if(i == 0) {
157+
$('.alert-danger', form).show().text(text);
158+
this.focus();
159+
}
160+
}
161+
});
162+
},
163+
});
164+
}
165+
166+
}( jQuery ));

0 commit comments

Comments
 (0)