ソースを参照

#393: The remote validator adds support for dynamic url and method type (GET/POST), thanks to @ericnakagawa

nghuuphuoc 11 年 前
コミット
123c93b30d
5 ファイル変更32 行追加19 行削除
  1. 1 0
      CHANGELOG.md
  2. 1 0
      README.md
  3. 17 9
      dist/js/bootstrapValidator.js
  4. 2 2
      dist/js/bootstrapValidator.min.js
  5. 11 8
      src/js/validator/remote.js

+ 1 - 0
CHANGELOG.md

@@ -39,6 +39,7 @@ __Improvements__
 * [#366](https://github.com/nghuuphuoc/bootstrapvalidator/issues/366): Don't change the enable setting when the new one is the same
 * [#371](https://github.com/nghuuphuoc/bootstrapvalidator/pull/371): Add H character to the Canadian postcode, thanks to [@jzhang6](https://github.com/jzhang6)
 * [#388](https://github.com/nghuuphuoc/bootstrapvalidator/issues/388): Allow to override the default options. Useful for using multiple forms in the same page
+* [#393](https://github.com/nghuuphuoc/bootstrapvalidator/pull/393): The [remote validator](http://bootstrapvalidator.com/validators/remote/) adds support for dynamic ```url``` and method type (GET/POST), thanks to [@ericnakagawa](https://github.com/ericnakagawa)
 
 __Bug Fixes__
 * [#288](https://github.com/nghuuphuoc/bootstrapvalidator/issues/288): Fix [```date``` validator](http://bootstrapvalidator.com/validators/date/) issue on IE8

+ 1 - 0
README.md

@@ -101,6 +101,7 @@ Big thanks to the contributors:
 * [@alavers](https://github.com/alavers)
 * [@easonhan007](https://github.com/easonhan007)
 * [@emilchristensen](https://github.com/emilchristensen)
+* [@ericnakagawa](https://github.com/ericnakagawa)
 * [@evilchili](https://github.com/evilchili)
 * [@Francismori7](https://github.com/Francismori7)
 * [@gercheq](https://github.com/gercheq)

+ 17 - 9
dist/js/bootstrapValidator.js

@@ -3897,12 +3897,13 @@
          * @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
+         * - 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>
          *  }
-         * - name [optional]: Override the field name for the request.
+         * - name {String} [optional]: Override the field name for the request.
          * - message: The invalid message
          * @returns {Boolean|Deferred}
          */
@@ -3912,20 +3913,27 @@
                 return true;
             }
 
-            var name = $field.attr('data-bv-field'), data = options.data;
-            if (data == null) {
-                data = {};
-            }
+            var name = $field.attr('data-bv-field'),
+                data = options.data || {},
+                url  = options.url,
+                type = options.type || 'POST';
+
             // Support dynamic data
             if ('function' == typeof data) {
                 data = data.call(this, validator);
             }
+
+            // Support dynamic url
+            if ('function' == typeof url) {
+                url = url.call(this, validator);
+            }
+
             data[options.name || name] = value;
 
             var dfd = new $.Deferred();
             var xhr = $.ajax({
-                type: 'POST',
-                url: options.url,
+                type: type,
+                url: url,
                 dataType: 'json',
                 data: data
             });

ファイルの差分が大きいため隠しています
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 11 - 8
src/js/validator/remote.js

@@ -12,12 +12,13 @@
          * @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
+         * - 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>
          *  }
-         * - name [optional]: Override the field name for the request.
+         * - name {String} [optional]: Override the field name for the request.
          * - message: The invalid message
          * @returns {Boolean|Deferred}
          */
@@ -27,19 +28,21 @@
                 return true;
             }
 
-            var name = $field.attr('data-bv-field'), data = options.data, url = options.url, type = options.type;
-            if (type == null || type != "GET") type = "POST";
-            if (data == null) {
-                data = {};
-            }
+            var name = $field.attr('data-bv-field'),
+                data = options.data || {},
+                url  = options.url,
+                type = options.type || 'POST';
+
             // Support dynamic data
             if ('function' == typeof data) {
                 data = data.call(this, validator);
             }
+
             // Support dynamic url
             if ('function' == typeof url) {
                 url = url.call(this, validator);
             }
+
             data[options.name || name] = value;
 
             var dfd = new $.Deferred();