1.仿照代码重写了原usernameCheck的validator。重命名为remoteCSRF,用于使用ajax异步向服务器验证字段唯一性。参数为url:请求地址,field:向服务器发送的表示符名称;

2.添加了emailAddress的validator的requires.js的config.
[ci skip]
This commit is contained in:
sxw 2015-08-05 19:38:28 +08:00
parent 0b55f6bbf7
commit 34ccdd5e0e
4 changed files with 51 additions and 40 deletions

View File

@ -26,8 +26,9 @@ var require = {
"validator/date": "lib/formValidation/validator/date", "validator/date": "lib/formValidation/validator/date",
"validator/integer": "lib/formValidation/validator/integer", "validator/integer": "lib/formValidation/validator/integer",
"validator/between": "lib/formValidation/validator/between", "validator/between": "lib/formValidation/validator/between",
'validator/confirm':"lib/formValidation/validator/confirm", "validator/confirm":"lib/formValidation/validator/confirm",
"validator/usernameCheck":"lib/formValidation/validator/usernameCheck", "validator/remoteCSRF":"lib/formValidation/validator/remoteCSRF",
"validator/emailAddress":"lib/formValidation/validator/emailAddress",
//富文本编辑器 不要直接使用而是使用上面的editor //富文本编辑器 不要直接使用而是使用上面的editor
simditor: "lib/simditor/simditor", simditor: "lib/simditor/simditor",
"simple-module": "lib/simditor/module", "simple-module": "lib/simditor/module",

View 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;
}
};
}));

View File

@ -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;
}
};
}));

View File

@ -9,6 +9,7 @@ define("validation",
'validator/integer', 'validator/integer',
'validator/between', 'validator/between',
'validator/confirm', 'validator/confirm',
'validator/usernameCheck'], 'validator/remoteCSRF',
'validator/emailAddress'],
function () { function () {
}); });