浏览代码

Allow the override of the field name for the remote request

Jonathan Swale 11 年之前
父节点
当前提交
b98bb90f9a
共有 1 个文件被更改,包括 51 次插入49 次删除
  1. 51 49
      src/js/validator/remote.js

+ 51 - 49
src/js/validator/remote.js

@@ -1,56 +1,58 @@
 (function($) {
-    $.fn.bootstrapValidator.validators.remote = {
-        html5Attributes: {
-            message: 'message',
-            url: 'url'
-        },
+		$.fn.bootstrapValidator.validators.remote = {
+				html5Attributes: {
+						message: 'message',
+						url: 'url',
+			name : 'name'
+				},
 
-        /**
-         * Request a remote server to check the input value
-         *
-         * @param {BootstrapValidator} validator Plugin instance
-         * @param {jQuery} $field Field element
-         * @param {Object} options Can consist of the following keys:
-         * - url
-         * - data [optional]: By default, it will take the value
-         *  {
-         *      <fieldName>: <fieldValue>
-         *  }
-         * - message: The invalid message
-         * @returns {Boolean|Deferred}
-         */
-        validate: function(validator, $field, options) {
-            var value = $field.val();
-            if (value == '') {
-                return true;
-            }
+				/**
+				 * Request a remote server to check the input value
+				 *
+				 * @param {BootstrapValidator} validator Plugin instance
+				 * @param {jQuery} $field Field element
+				 * @param {Object} options Can consist of the following keys:
+				 * - url
+				 * - data [optional]: By default, it will take the value
+				 *  {
+				 *      <fieldName>: <fieldValue>
+				 *  }
+				 * - name [optional]: Override the field name for the request.
+				 * - message: The invalid message
+				 * @returns {Boolean|Deferred}
+				 */
+				validate: function(validator, $field, options) {
+						var value = $field.val();
+						if (value == '') {
+								return true;
+						}
 
-            var name = $field.attr('data-bv-field'), data = options.data;
-            if (data == null) {
-                data = {};
-            }
-            // Support dynamic data
-            if ('function' == typeof data) {
-                data = data.call(this, validator);
-            }
-            data[name] = value;
+						var name = $field.attr('data-bv-field'), data = options.data;
+						if (data == null) {
+								data = {};
+						}
+						// Support dynamic data
+						if ('function' == typeof data) {
+								data = data.call(this, validator);
+						}
+						data[options.name || name] = value;
 
-            var dfd = new $.Deferred();
-            var xhr = $.ajax({
-                type: 'POST',
-                url: options.url,
-                dataType: 'json',
-                data: data
-            });
-            xhr.then(function(response) {
-                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
-            });
+						var dfd = new $.Deferred();
+						var xhr = $.ajax({
+								type: 'POST',
+								url: options.url,
+								dataType: 'json',
+								data: data
+						});
+						xhr.then(function(response) {
+								dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
+						});
 
-            dfd.fail(function() {
-                xhr.abort();
-            });
+						dfd.fail(function() {
+								xhr.abort();
+						});
 
-            return dfd;
-        }
-    };
+						return dfd;
+				}
+		};
 }(window.jQuery));