浏览代码

#125, #130: Combine removeField() and removeFieldElement() methods

phuoc 11 年之前
父节点
当前提交
9695a6fe10
共有 5 个文件被更改,包括 99 次插入106 次删除
  1. 1 1
      demo/dynamic.html
  2. 2 3
      demo/dynamic3.html
  3. 47 50
      dist/js/bootstrapValidator.js
  4. 2 2
      dist/js/bootstrapValidator.min.js
  5. 47 50
      src/js/bootstrapValidator.js

+ 1 - 1
demo/dynamic.html

@@ -111,7 +111,7 @@
             }
 
             $row.on('click', '.removeButton', function(e) {
-                $('#defaultForm').bootstrapValidator('removeFieldElement', $el);
+                $('#defaultForm').bootstrapValidator('removeField', $el);
                 $row.remove();
             });
         });

+ 2 - 3
demo/dynamic3.html

@@ -162,9 +162,8 @@
                         // Use removeField() method
                         .bootstrapValidator('removeField', 'receiverName')
                         .bootstrapValidator('removeField', 'receiverAddress')
-                        // Use removeFieldElement() method
-                        .bootstrapValidator('removeFieldElement', $receiverPhone)
-                        .bootstrapValidator('removeFieldElement', $receiverCity);
+                        .bootstrapValidator('removeField', $receiverPhone)
+                        .bootstrapValidator('removeField', $receiverCity);
                 } else {
                     $('#receiverInfo').show();
 

+ 47 - 50
dist/js/bootstrapValidator.js

@@ -596,12 +596,10 @@
                 }
             }
 
-            var index = this.$invalidFields.index($field);
             if (counter[this.STATUS_VALID] == numValidators) {
                 // Remove from the list of invalid fields
-                if (index != -1) {
-                    this.$invalidFields.splice(index, 1);
-                }
+                this.$invalidFields = this.$invalidFields.not($field);
+
                 this.$form.trigger($.Event('success.field.bv'), {
                     field: field,
                     element: $field
@@ -610,9 +608,8 @@
             // If all validators are completed and there is at least one validator which doesn't pass
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
                 // Add to the list of invalid fields
-                if (index == -1) {
-                    this.$invalidFields = this.$invalidFields.add($field);
-                }
+                this.$invalidFields = this.$invalidFields.add($field);
+
                 this.$form.trigger($.Event('error.field.bv'), {
                     field: field,
                     element: $field
@@ -695,7 +692,7 @@
                     break;
             }
 
-            if (this.options.fields[field]['enabled'] == false) {
+            if (this.options.fields[field] && this.options.fields[field]['enabled'] == false) {
                 return this;
             }
 
@@ -1083,18 +1080,54 @@
         /**
          * Remove a given field
          *
-         * @param {String} field The field name
+         * @param {String|jQuery} field The field name or field element
          * @returns {BootstrapValidator}
          */
         removeField: function(field) {
-            var fields = this.getFieldElements(field),
-                type   = fields.attr('type'),
-                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+            var fields = $([]);
+            switch (typeof field) {
+                case 'object':
+                    fields = field;
+                    field  = field.attr('data-bv-field') || field.attr('name');
+                    fields.attr('data-bv-field', field);
+                    break;
+                case 'string':
+                    fields = this.getFieldElements(field);
+                    break;
+                default:
+                    break;
+            }
 
-            for (var i = 0; i < n; i++) {
-                this.removeFieldElement($(fields[i]));
+            if (fields.length == 0) {
+                return this;
+            }
+
+            var type  = fields.attr('type'),
+                total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
+
+            for (var i = 0; i < total; i++) {
+                var $field = $(fields[i]);
+
+                // Remove from the list of invalid fields
+                this.$invalidFields = this.$invalidFields.not($field);
+
+                // Update the cache
+                this._cacheFields[field] = this._cacheFields[field].not($field);
             }
 
+            if (!this._cacheFields[field] || this._cacheFields[field].length == 0) {
+                delete this.options.fields[field];
+            }
+            if ('checkbox' == type || 'radio' == type) {
+                this._initField(field);
+            }
+
+            // Trigger an event
+            this.$form.trigger($.Event('removed.field.bv'), {
+                field: field,
+                element: fields
+            });
+
             this.disableSubmitButtons(false);
             return this;
         },
@@ -1134,42 +1167,6 @@
         },
 
         /**
-         * Remove given field element
-         *
-         * @param {jQuery} $field The field element
-         * @returns {BootstrapValidator}
-         */
-        removeFieldElement: function($field) {
-            var field = $field.attr('data-bv-field') || $field.attr('name'),
-                type  = $field.attr('type');
-
-            $field.attr('data-bv-field', field);
-
-            // Remove from the list of invalid fields
-            var index = this.$invalidFields.index($field);
-            if (index != -1) {
-                this.$invalidFields.splice(index, 1);
-            }
-
-            // Update the cache
-            $field.remove();
-            delete this._cacheFields[field];
-            if ('checkbox' == type || 'radio' == type) {
-                this._initField(field);
-            }
-
-            this.disableSubmitButtons(false);
-
-            // Trigger an event
-            this.$form.trigger($.Event('removed.field.bv'), {
-                field: field,
-                element: $field
-            });
-
-            return this;
-        },
-
-        /**
          * Reset the form
          *
          * @param {Boolean} [resetFormData] Reset current form data

文件差异内容过多而无法显示
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 47 - 50
src/js/bootstrapValidator.js

@@ -595,12 +595,10 @@
                 }
             }
 
-            var index = this.$invalidFields.index($field);
             if (counter[this.STATUS_VALID] == numValidators) {
                 // Remove from the list of invalid fields
-                if (index != -1) {
-                    this.$invalidFields.splice(index, 1);
-                }
+                this.$invalidFields = this.$invalidFields.not($field);
+
                 this.$form.trigger($.Event('success.field.bv'), {
                     field: field,
                     element: $field
@@ -609,9 +607,8 @@
             // If all validators are completed and there is at least one validator which doesn't pass
             else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
                 // Add to the list of invalid fields
-                if (index == -1) {
-                    this.$invalidFields = this.$invalidFields.add($field);
-                }
+                this.$invalidFields = this.$invalidFields.add($field);
+
                 this.$form.trigger($.Event('error.field.bv'), {
                     field: field,
                     element: $field
@@ -694,7 +691,7 @@
                     break;
             }
 
-            if (this.options.fields[field]['enabled'] == false) {
+            if (this.options.fields[field] && this.options.fields[field]['enabled'] == false) {
                 return this;
             }
 
@@ -1082,18 +1079,54 @@
         /**
          * Remove a given field
          *
-         * @param {String} field The field name
+         * @param {String|jQuery} field The field name or field element
          * @returns {BootstrapValidator}
          */
         removeField: function(field) {
-            var fields = this.getFieldElements(field),
-                type   = fields.attr('type'),
-                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+            var fields = $([]);
+            switch (typeof field) {
+                case 'object':
+                    fields = field;
+                    field  = field.attr('data-bv-field') || field.attr('name');
+                    fields.attr('data-bv-field', field);
+                    break;
+                case 'string':
+                    fields = this.getFieldElements(field);
+                    break;
+                default:
+                    break;
+            }
 
-            for (var i = 0; i < n; i++) {
-                this.removeFieldElement($(fields[i]));
+            if (fields.length == 0) {
+                return this;
+            }
+
+            var type  = fields.attr('type'),
+                total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
+
+            for (var i = 0; i < total; i++) {
+                var $field = $(fields[i]);
+
+                // Remove from the list of invalid fields
+                this.$invalidFields = this.$invalidFields.not($field);
+
+                // Update the cache
+                this._cacheFields[field] = this._cacheFields[field].not($field);
+            }
+
+            if (!this._cacheFields[field] || this._cacheFields[field].length == 0) {
+                delete this.options.fields[field];
+            }
+            if ('checkbox' == type || 'radio' == type) {
+                this._initField(field);
             }
 
+            // Trigger an event
+            this.$form.trigger($.Event('removed.field.bv'), {
+                field: field,
+                element: fields
+            });
+
             this.disableSubmitButtons(false);
             return this;
         },
@@ -1133,42 +1166,6 @@
         },
 
         /**
-         * Remove given field element
-         *
-         * @param {jQuery} $field The field element
-         * @returns {BootstrapValidator}
-         */
-        removeFieldElement: function($field) {
-            var field = $field.attr('data-bv-field') || $field.attr('name'),
-                type  = $field.attr('type');
-
-            $field.attr('data-bv-field', field);
-
-            // Remove from the list of invalid fields
-            var index = this.$invalidFields.index($field);
-            if (index != -1) {
-                this.$invalidFields.splice(index, 1);
-            }
-
-            // Update the cache
-            $field.remove();
-            delete this._cacheFields[field];
-            if ('checkbox' == type || 'radio' == type) {
-                this._initField(field);
-            }
-
-            this.disableSubmitButtons(false);
-
-            // Trigger an event
-            this.$form.trigger($.Event('removed.field.bv'), {
-                field: field,
-                element: $field
-            });
-
-            return this;
-        },
-
-        /**
          * Reset the form
          *
          * @param {Boolean} [resetFormData] Reset current form data