mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-09-21 08:23:20 +00:00
1.仿照代码重写了原usernameCheck的validator。重命名为remoteCSRF,用于使用ajax异步向服务器验证字段唯一性。参数为url:请求地址,field:向服务器发送的表示符名称;
2.添加了emailAddress的validator的requires.js的config. [ci skip]
This commit is contained in:
parent
0b55f6bbf7
commit
34ccdd5e0e
@ -26,8 +26,9 @@ var require = {
|
||||
"validator/date": "lib/formValidation/validator/date",
|
||||
"validator/integer": "lib/formValidation/validator/integer",
|
||||
"validator/between": "lib/formValidation/validator/between",
|
||||
'validator/confirm':"lib/formValidation/validator/confirm",
|
||||
"validator/usernameCheck":"lib/formValidation/validator/usernameCheck",
|
||||
"validator/confirm":"lib/formValidation/validator/confirm",
|
||||
"validator/remoteCSRF":"lib/formValidation/validator/remoteCSRF",
|
||||
"validator/emailAddress":"lib/formValidation/validator/emailAddress",
|
||||
//富文本编辑器 不要直接使用,而是使用上面的editor
|
||||
simditor: "lib/simditor/simditor",
|
||||
"simple-module": "lib/simditor/module",
|
||||
|
46
static/src/js/lib/formValidation/validator/remoteCSRF.js
Normal file
46
static/src/js/lib/formValidation/validator/remoteCSRF.js
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* remoteCSRF validator
|
||||
*/
|
||||
(function(root, factory) {
|
||||
|
||||
"use strict";
|
||||
|
||||
// AMD module is defined
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("validator/remoteCSRF", ["jquery", "base", "csrf"], factory);
|
||||
} else {
|
||||
// planted over the root!
|
||||
factory(root.jQuery, root.FormValidation);
|
||||
}
|
||||
}(this, function ($, FormValidation, csrfHeader) {
|
||||
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
||||
'en_US': {
|
||||
remoteCSRF: {
|
||||
'default': ''
|
||||
}
|
||||
}
|
||||
});
|
||||
FormValidation.Validator.remoteCSRF = {
|
||||
validate: function(validator, $field, options) {
|
||||
var dfd = new $.Deferred(), ajaxData = {};
|
||||
ajaxData[options.field] = $field.val();
|
||||
if ($field.val() === '')
|
||||
return true;
|
||||
var url = options.url;
|
||||
var xhr = $.ajax({
|
||||
beforeSend: csrfHeader,
|
||||
url: url,
|
||||
dataType: 'json',
|
||||
data: ajaxData,
|
||||
method: "post"
|
||||
});
|
||||
xhr.success(function(response) {
|
||||
dfd.resolve($field, 'remoteCSRF',{valid:!response.data, message:options.msg});
|
||||
})
|
||||
.error(function(response) {
|
||||
dfd.resolve($field, 'remoteCSRF', {valid: false});
|
||||
});
|
||||
return dfd;
|
||||
}
|
||||
};
|
||||
}));
|
@ -1,37 +0,0 @@
|
||||
/**
|
||||
* usernameCheck validator
|
||||
*/
|
||||
(function(root, factory) {
|
||||
|
||||
"use strict";
|
||||
|
||||
// AMD module is defined
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("validator/usernameCheck", ["jquery", "base", "csrf"], factory);
|
||||
} else {
|
||||
// planted over the root!
|
||||
factory(root.jQuery, root.FormValidation);
|
||||
}
|
||||
}(this, function ($, FormValidation, csrfHeader) {
|
||||
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
||||
'en_US': {
|
||||
usernameCheck: {
|
||||
'default': 'Please input the same value'
|
||||
}
|
||||
}
|
||||
});
|
||||
FormValidation.Validator.usernameCheck = {
|
||||
validate: function(validator, $field, options) {
|
||||
if ($field.val() == '')
|
||||
return true;
|
||||
return !$.ajax({
|
||||
async: false,
|
||||
beforeSend: csrfHeader,
|
||||
url: "/api/username_check/",
|
||||
data: {username: $field.val()},
|
||||
dataType: "json",
|
||||
method: "post",
|
||||
}).responseJSON.data;
|
||||
}
|
||||
};
|
||||
}));
|
@ -9,6 +9,7 @@ define("validation",
|
||||
'validator/integer',
|
||||
'validator/between',
|
||||
'validator/confirm',
|
||||
'validator/usernameCheck'],
|
||||
'validator/remoteCSRF',
|
||||
'validator/emailAddress'],
|
||||
function () {
|
||||
});
|
Loading…
Reference in New Issue
Block a user