ソースを参照

#136: Remove this.dfs variable. Store the validate result for each field via data()

nghuuphuoc 11 年 前
コミット
644cc8cbc0

+ 4 - 4
demo/index.html

@@ -234,10 +234,10 @@ $(document).ready(function() {
                         regexp: /^[a-zA-Z0-9_\.]+$/,
                         message: 'The username can only consist of alphabetical, number, dot and underscore'
                     },
-//                    remote: {
-//                        url: 'remote.php',
-//                        message: 'The username is not available'
-//                    },
+                    remote: {
+                        url: 'remote.php',
+                        message: 'The username is not available'
+                    },
                     different: {
                         field: 'password',
                         message: 'The username and password cannot be the same as each other'

+ 14 - 15
dist/js/bootstrapValidator.js

@@ -14,8 +14,7 @@
         this.$form   = $(form);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
 
-        this.dfds    = {};      // Array of deferred
-        this.results = {};      // Validating results
+        this.results = {};          // Validating results
 
         this.invalidField  = null;  // First invalid field
         this.$submitButton = null;  // The submit button which is clicked to submit form
@@ -189,7 +188,6 @@
                 return;
             }
 
-            this.dfds[field]    = {};
             this.results[field] = {};
 
             var fields = this.getFieldElements(field);
@@ -197,11 +195,13 @@
             // We don't need to validate non-existing fields
             if (fields == null) {
                 delete this.options.fields[field];
-                delete this.dfds[field];
                 return;
             }
 
-            fields.attr('data-bv-field', field);
+            // Set the attribute to indicate the fields which are defined by selector
+            if (fields.attr('data-bv-field')) {
+                fields.attr('data-bv-field', field);
+            }
 
             // Create help block elements for showing the error messages
             var $field   = $(fields[0]),
@@ -403,13 +403,12 @@
             // We don't need to validate disabled field
             if (fields.length == 1 && fields.is(':disabled')) {
                 delete this.options.fields[field];
-                delete this.dfds[field];
                 return;
             }
 
             for (validatorName in validators) {
-                if (this.dfds[field][validatorName]) {
-                    this.dfds[field][validatorName].reject();
+                if ($field.data('bv.dfs.' + validatorName)) {
+                    $field.data('bv.dfs.' + validatorName).reject();
                 }
 
                 // Don't validate field if it is already done
@@ -422,12 +421,12 @@
 
                 if ('object' == typeof validateResult) {
                     this.updateStatus($field, this.STATUS_VALIDATING, validatorName);
-                    this.dfds[field][validatorName] = validateResult;
+                    $field.data('bv.dfs.' + validatorName, validateResult);
 
-                    validateResult.done(function(isValid, v) {
+                    validateResult.done(function($f, v, isValid) {
                         // v is validator name
-                        delete that.dfds[field][v];
-                        that.updateStatus($field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
+                        $f.removeData('bv.dfs.' + v);
+                        that.updateStatus(field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
 
                         if (isValid && 'disabled' == that.options.live) {
                             that._submit();
@@ -556,10 +555,10 @@
         resetForm: function(resetFormData) {
             var field, $field, type;
             for (field in this.options.fields) {
-                this.dfds[field]    = {};
                 this.results[field] = {};
 
                 $field = this.getFieldElements(field);
+                $field.removeData('bv.dfs');
                 // Mark field as not validated yet
                 this.updateStatus($field, this.STATUS_NOT_VALIDATED, null);
 
@@ -679,7 +678,7 @@
             var value = $field.val();
             if (options.callback && 'function' == typeof options.callback) {
                 var dfd = new $.Deferred();
-                dfd.resolve(options.callback.call(this, value, validator), 'callback');
+                dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
                 return dfd;
             }
             return true;
@@ -1538,7 +1537,7 @@
                 data: data
             });
             xhr.then(function(response) {
-                dfd.resolve(response.valid === true || response.valid === 'true', 'remote');
+                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
             });
 
             dfd.fail(function() {

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


+ 12 - 13
src/js/bootstrapValidator.js

@@ -13,8 +13,7 @@
         this.$form   = $(form);
         this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
 
-        this.dfds    = {};      // Array of deferred
-        this.results = {};      // Validating results
+        this.results = {};          // Validating results
 
         this.invalidField  = null;  // First invalid field
         this.$submitButton = null;  // The submit button which is clicked to submit form
@@ -188,7 +187,6 @@
                 return;
             }
 
-            this.dfds[field]    = {};
             this.results[field] = {};
 
             var fields = this.getFieldElements(field);
@@ -196,11 +194,13 @@
             // We don't need to validate non-existing fields
             if (fields == null) {
                 delete this.options.fields[field];
-                delete this.dfds[field];
                 return;
             }
 
-            fields.attr('data-bv-field', field);
+            // Set the attribute to indicate the fields which are defined by selector
+            if (fields.attr('data-bv-field')) {
+                fields.attr('data-bv-field', field);
+            }
 
             // Create help block elements for showing the error messages
             var $field   = $(fields[0]),
@@ -402,13 +402,12 @@
             // We don't need to validate disabled field
             if (fields.length == 1 && fields.is(':disabled')) {
                 delete this.options.fields[field];
-                delete this.dfds[field];
                 return;
             }
 
             for (validatorName in validators) {
-                if (this.dfds[field][validatorName]) {
-                    this.dfds[field][validatorName].reject();
+                if ($field.data('bv.dfs.' + validatorName)) {
+                    $field.data('bv.dfs.' + validatorName).reject();
                 }
 
                 // Don't validate field if it is already done
@@ -421,12 +420,12 @@
 
                 if ('object' == typeof validateResult) {
                     this.updateStatus($field, this.STATUS_VALIDATING, validatorName);
-                    this.dfds[field][validatorName] = validateResult;
+                    $field.data('bv.dfs.' + validatorName, validateResult);
 
-                    validateResult.done(function(isValid, v) {
+                    validateResult.done(function($f, v, isValid) {
                         // v is validator name
-                        delete that.dfds[field][v];
-                        that.updateStatus($field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
+                        $f.removeData('bv.dfs.' + v);
+                        that.updateStatus(field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
 
                         if (isValid && 'disabled' == that.options.live) {
                             that._submit();
@@ -555,10 +554,10 @@
         resetForm: function(resetFormData) {
             var field, $field, type;
             for (field in this.options.fields) {
-                this.dfds[field]    = {};
                 this.results[field] = {};
 
                 $field = this.getFieldElements(field);
+                $field.removeData('bv.dfs');
                 // Mark field as not validated yet
                 this.updateStatus($field, this.STATUS_NOT_VALIDATED, null);
 

+ 1 - 1
src/js/validator/callback.js

@@ -18,7 +18,7 @@
             var value = $field.val();
             if (options.callback && 'function' == typeof options.callback) {
                 var dfd = new $.Deferred();
-                dfd.resolve(options.callback.call(this, value, validator), 'callback');
+                dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
                 return dfd;
             }
             return true;

+ 1 - 1
src/js/validator/remote.js

@@ -40,7 +40,7 @@
                 data: data
             });
             xhr.then(function(response) {
-                dfd.resolve(response.valid === true || response.valid === 'true', 'remote');
+                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
             });
 
             dfd.fail(function() {