更正了validator命名的问题,remoteCSRF->remote.把原生的remote改成了remotex。。。
[ci skip]
This commit is contained in:
@@ -18,8 +18,10 @@ require(["jquery", "bs_alert", "csrf", "validation"], function($, bs_alert, csrf
|
|||||||
max: 30,
|
max: 30,
|
||||||
message: '用户名长度必须在3到30位之间'
|
message: '用户名长度必须在3到30位之间'
|
||||||
},
|
},
|
||||||
usernameCheck:{
|
remote: {
|
||||||
message: '用户名已存在'
|
message: "用户名已存在",
|
||||||
|
url: "/api/username_check/",
|
||||||
|
field: 'username'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -64,7 +66,7 @@ require(["jquery", "bs_alert", "csrf", "validation"], function($, bs_alert, csrf
|
|||||||
emailAddress: {
|
emailAddress: {
|
||||||
message: "请填写有效的邮箱地址"
|
message: "请填写有效的邮箱地址"
|
||||||
},
|
},
|
||||||
remoteCSRF: {
|
remote: {
|
||||||
message: "您已经注册过了",
|
message: "您已经注册过了",
|
||||||
url: "/api/email_check/",
|
url: "/api/email_check/",
|
||||||
field: 'email'
|
field: 'email'
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ var require = {
|
|||||||
"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/remoteCSRF":"lib/formValidation/validator/remoteCSRF",
|
"validator/remote":"lib/formValidation/validator/remote",
|
||||||
"validator/emailAddress":"lib/formValidation/validator/emailAddress",
|
"validator/emailAddress":"lib/formValidation/validator/emailAddress",
|
||||||
//富文本编辑器 不要直接使用,而是使用上面的editor
|
//富文本编辑器 不要直接使用,而是使用上面的editor
|
||||||
simditor: "lib/simditor/simditor",
|
simditor: "lib/simditor/simditor",
|
||||||
|
|||||||
@@ -1,146 +1,46 @@
|
|||||||
/**
|
/**
|
||||||
* remote validator
|
* remote validator
|
||||||
*
|
|
||||||
* @link http://formvalidation.io/validators/remote/
|
|
||||||
* @author https://twitter.com/nghuuphuoc
|
|
||||||
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
|
|
||||||
* @license http://formvalidation.io/license/
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function(root, factory) {
|
(function(root, factory) {
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// AMD module is defined
|
// AMD module is defined
|
||||||
if (typeof define === "function" && define.amd) {
|
if (typeof define === "function" && define.amd) {
|
||||||
define("validator/remote", ["jquery", "base"], factory);
|
define("validator/remote", ["jquery", "base", "csrf"], factory);
|
||||||
} else {
|
} else {
|
||||||
// planted over the root!
|
// planted over the root!
|
||||||
factory(root.jQuery, root.FormValidation);
|
factory(root.jQuery, root.FormValidation);
|
||||||
}
|
}
|
||||||
|
}(this, function ($, FormValidation, csrfHeader) {
|
||||||
}(this, function ($, FormValidation) {
|
|
||||||
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
||||||
'en_US': {
|
'en_US': {
|
||||||
remote: {
|
remote: {
|
||||||
'default': 'Please enter a valid value'
|
'default': ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
FormValidation.Validator.remote = {
|
FormValidation.Validator.remote = {
|
||||||
html5Attributes: {
|
|
||||||
message: 'message',
|
|
||||||
name: 'name',
|
|
||||||
type: 'type',
|
|
||||||
url: 'url',
|
|
||||||
data: 'data',
|
|
||||||
delay: 'delay'
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroy the timer when destroying the bootstrapValidator (using validator.destroy() method)
|
|
||||||
*/
|
|
||||||
destroy: function(validator, $field, options) {
|
|
||||||
var ns = validator.getNamespace(),
|
|
||||||
timer = $field.data(ns + '.remote.timer');
|
|
||||||
if (timer) {
|
|
||||||
clearTimeout(timer);
|
|
||||||
$field.removeData(ns + '.remote.timer');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request a remote server to check the input value
|
|
||||||
*
|
|
||||||
* @param {FormValidation.Base} validator Plugin instance
|
|
||||||
* @param {jQuery} $field Field element
|
|
||||||
* @param {Object} options Can consist of the following keys:
|
|
||||||
* - url {String|Function}
|
|
||||||
* - type {String} [optional] Can be GET or POST (default)
|
|
||||||
* - data {Object|Function} [optional]: By default, it will take the value
|
|
||||||
* {
|
|
||||||
* <fieldName>: <fieldValue>
|
|
||||||
* }
|
|
||||||
* - delay
|
|
||||||
* - name {String} [optional]: Override the field name for the request.
|
|
||||||
* - message: The invalid message
|
|
||||||
* - headers: Additional headers
|
|
||||||
* @returns {Deferred}
|
|
||||||
*/
|
|
||||||
validate: function(validator, $field, options) {
|
validate: function(validator, $field, options) {
|
||||||
var ns = validator.getNamespace(),
|
var dfd = new $.Deferred(), ajaxData = {};
|
||||||
value = validator.getFieldValue($field, 'remote'),
|
ajaxData[options.field] = $field.val();
|
||||||
dfd = new $.Deferred();
|
if ($field.val() === '')
|
||||||
if (value === '') {
|
return true;
|
||||||
dfd.resolve($field, 'remote', { valid: true });
|
var url = options.url;
|
||||||
return dfd;
|
var xhr = $.ajax({
|
||||||
}
|
beforeSend: csrfHeader,
|
||||||
|
url: url,
|
||||||
var name = $field.attr('data-' + ns + '-field'),
|
dataType: 'json',
|
||||||
data = options.data || {},
|
data: ajaxData,
|
||||||
url = options.url,
|
method: "post"
|
||||||
type = options.type || 'GET',
|
});
|
||||||
headers = options.headers || {};
|
xhr.success(function(response) {
|
||||||
|
dfd.resolve($field, 'remote',{valid:!response.data, message:options.msg});
|
||||||
// Support dynamic data
|
})
|
||||||
if ('function' === typeof data) {
|
.error(function(response) {
|
||||||
data = data.call(this, validator);
|
dfd.resolve($field, 'remote', {valid: false});
|
||||||
}
|
});
|
||||||
|
return dfd;
|
||||||
// Parse string data from HTML5 attribute
|
|
||||||
if ('string' === typeof data) {
|
|
||||||
data = JSON.parse(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Support dynamic url
|
|
||||||
if ('function' === typeof url) {
|
|
||||||
url = url.call(this, validator);
|
|
||||||
}
|
|
||||||
|
|
||||||
data[options.name || name] = value;
|
|
||||||
function runCallback() {
|
|
||||||
var xhr = $.ajax({
|
|
||||||
type: type,
|
|
||||||
headers: headers,
|
|
||||||
url: url,
|
|
||||||
dataType: 'json',
|
|
||||||
data: data
|
|
||||||
});
|
|
||||||
|
|
||||||
xhr
|
|
||||||
.success(function(response) {
|
|
||||||
response.valid = response.valid === true || response.valid === 'true';
|
|
||||||
dfd.resolve($field, 'remote', response);
|
|
||||||
})
|
|
||||||
.error(function(response) {
|
|
||||||
dfd.resolve($field, 'remote', {
|
|
||||||
valid: false
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
dfd.fail(function() {
|
|
||||||
xhr.abort();
|
|
||||||
});
|
|
||||||
|
|
||||||
return dfd;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.delay) {
|
|
||||||
// Since the form might have multiple fields with the same name
|
|
||||||
// I have to attach the timer to the field element
|
|
||||||
if ($field.data(ns + '.remote.timer')) {
|
|
||||||
clearTimeout($field.data(ns + '.remote.timer'));
|
|
||||||
}
|
|
||||||
|
|
||||||
$field.data(ns + '.remote.timer', setTimeout(runCallback, options.delay));
|
|
||||||
return dfd;
|
|
||||||
} else {
|
|
||||||
return runCallback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return FormValidation.Validator.remote;
|
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
146
static/src/js/lib/formValidation/validator/remotex.js
Normal file
146
static/src/js/lib/formValidation/validator/remotex.js
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
/**
|
||||||
|
* remote validator
|
||||||
|
*
|
||||||
|
* @link http://formvalidation.io/validators/remote/
|
||||||
|
* @author https://twitter.com/nghuuphuoc
|
||||||
|
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
|
||||||
|
* @license http://formvalidation.io/license/
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function(root, factory) {
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// AMD module is defined
|
||||||
|
if (typeof define === "function" && define.amd) {
|
||||||
|
define("validator/remote", ["jquery", "base"], factory);
|
||||||
|
} else {
|
||||||
|
// planted over the root!
|
||||||
|
factory(root.jQuery, root.FormValidation);
|
||||||
|
}
|
||||||
|
|
||||||
|
}(this, function ($, FormValidation) {
|
||||||
|
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
||||||
|
'en_US': {
|
||||||
|
remote: {
|
||||||
|
'default': 'Please enter a valid value'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
FormValidation.Validator.remote = {
|
||||||
|
html5Attributes: {
|
||||||
|
message: 'message',
|
||||||
|
name: 'name',
|
||||||
|
type: 'type',
|
||||||
|
url: 'url',
|
||||||
|
data: 'data',
|
||||||
|
delay: 'delay'
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the timer when destroying the bootstrapValidator (using validator.destroy() method)
|
||||||
|
*/
|
||||||
|
destroy: function(validator, $field, options) {
|
||||||
|
var ns = validator.getNamespace(),
|
||||||
|
timer = $field.data(ns + '.remote.timer');
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer);
|
||||||
|
$field.removeData(ns + '.remote.timer');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request a remote server to check the input value
|
||||||
|
*
|
||||||
|
* @param {FormValidation.Base} validator Plugin instance
|
||||||
|
* @param {jQuery} $field Field element
|
||||||
|
* @param {Object} options Can consist of the following keys:
|
||||||
|
* - url {String|Function}
|
||||||
|
* - type {String} [optional] Can be GET or POST (default)
|
||||||
|
* - data {Object|Function} [optional]: By default, it will take the value
|
||||||
|
* {
|
||||||
|
* <fieldName>: <fieldValue>
|
||||||
|
* }
|
||||||
|
* - delay
|
||||||
|
* - name {String} [optional]: Override the field name for the request.
|
||||||
|
* - message: The invalid message
|
||||||
|
* - headers: Additional headers
|
||||||
|
* @returns {Deferred}
|
||||||
|
*/
|
||||||
|
validate: function(validator, $field, options) {
|
||||||
|
var ns = validator.getNamespace(),
|
||||||
|
value = validator.getFieldValue($field, 'remote'),
|
||||||
|
dfd = new $.Deferred();
|
||||||
|
if (value === '') {
|
||||||
|
dfd.resolve($field, 'remote', { valid: true });
|
||||||
|
return dfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
var name = $field.attr('data-' + ns + '-field'),
|
||||||
|
data = options.data || {},
|
||||||
|
url = options.url,
|
||||||
|
type = options.type || 'GET',
|
||||||
|
headers = options.headers || {};
|
||||||
|
|
||||||
|
// Support dynamic data
|
||||||
|
if ('function' === typeof data) {
|
||||||
|
data = data.call(this, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse string data from HTML5 attribute
|
||||||
|
if ('string' === typeof data) {
|
||||||
|
data = JSON.parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support dynamic url
|
||||||
|
if ('function' === typeof url) {
|
||||||
|
url = url.call(this, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
data[options.name || name] = value;
|
||||||
|
function runCallback() {
|
||||||
|
var xhr = $.ajax({
|
||||||
|
type: type,
|
||||||
|
headers: headers,
|
||||||
|
url: url,
|
||||||
|
dataType: 'json',
|
||||||
|
data: data
|
||||||
|
});
|
||||||
|
|
||||||
|
xhr
|
||||||
|
.success(function(response) {
|
||||||
|
response.valid = response.valid === true || response.valid === 'true';
|
||||||
|
dfd.resolve($field, 'remote', response);
|
||||||
|
})
|
||||||
|
.error(function(response) {
|
||||||
|
dfd.resolve($field, 'remote', {
|
||||||
|
valid: false
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
dfd.fail(function() {
|
||||||
|
xhr.abort();
|
||||||
|
});
|
||||||
|
|
||||||
|
return dfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.delay) {
|
||||||
|
// Since the form might have multiple fields with the same name
|
||||||
|
// I have to attach the timer to the field element
|
||||||
|
if ($field.data(ns + '.remote.timer')) {
|
||||||
|
clearTimeout($field.data(ns + '.remote.timer'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$field.data(ns + '.remote.timer', setTimeout(runCallback, options.delay));
|
||||||
|
return dfd;
|
||||||
|
} else {
|
||||||
|
return runCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
return FormValidation.Validator.remote;
|
||||||
|
}));
|
||||||
@@ -9,7 +9,7 @@ define("validation",
|
|||||||
'validator/integer',
|
'validator/integer',
|
||||||
'validator/between',
|
'validator/between',
|
||||||
'validator/confirm',
|
'validator/confirm',
|
||||||
'validator/remoteCSRF',
|
'validator/remote',
|
||||||
'validator/emailAddress'],
|
'validator/emailAddress'],
|
||||||
function () {
|
function () {
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user