浏览代码

#130: Add addField() and removeField() methods for managing dynamic fields, thanks to @jcnmulio

phuoc 11 年之前
父节点
当前提交
927242693f
共有 5 个文件被更改,包括 82 次插入36 次删除
  1. 1 0
      CHANGELOG.md
  2. 1 0
      README.md
  3. 39 1
      dist/js/bootstrapValidator.js
  4. 2 2
      dist/js/bootstrapValidator.min.js
  5. 39 33
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 * Add ```status.field.bv``` event which is triggered after updating the field status. It can be used to solve [#300](https://github.com/nghuuphuoc/bootstrapvalidator/issues/300), [#301](https://github.com/nghuuphuoc/bootstrapvalidator/issues/301)
 * [#164](https://github.com/nghuuphuoc/bootstrapvalidator/issues/164): Add ```container``` option for indicating the element showing all errors
 * [#175](https://github.com/nghuuphuoc/bootstrapvalidator/issues/175): Showing errors in tooltip or popover
+* [#130](https://github.com/nghuuphuoc/bootstrapvalidator/pull/130): Add ```addField()``` and ```removeField()``` methods for managing dynamic fields, thanks to [@jcnmulio](https://github.com/jcnmulio)
 * [#211](https://github.com/nghuuphuoc/bootstrapvalidator/issues/211), [#235](https://github.com/nghuuphuoc/bootstrapvalidator/issues/235): Add new method ```getInvalidFields()``` that returns all invalid fields
 * [#275](https://github.com/nghuuphuoc/bootstrapvalidator/issues/275): Add ```destroy()``` method
 * [#316](https://github.com/nghuuphuoc/bootstrapvalidator/issues/316): Add ```isValidContainer()``` method

+ 1 - 0
README.md

@@ -106,6 +106,7 @@ Big thanks to the contributors:
 * [@henningda](https://github.com/henningda)
 * [@ikanedo](https://github.com/ikanedo)
 * [@iplus](https://github.com/iplus)
+* [@jcnmulio](https://github.com/jcnmulio)
 * [@johanronn77](https://github.com/johanronn77)
 * [@jswale](https://github.com/jswale)
 * [@khangvm53](https://github.com/khangvm53)

+ 39 - 1
dist/js/bootstrapValidator.js

@@ -1017,7 +1017,27 @@
                 this.$invalidFields.splice(index, 1);
             }
 
-            if ('checkbox' == type || 'radio' == type) {
+            if (this._cacheFields[field].length == 0) {
+                // There is no field with the same name
+                delete this.options.fields[field];
+                delete this._cacheFields[field];
+            } else if ('checkbox' == type || 'radio' == type) {
+                this._initField(field);
+            }
+
+            return this;
+        },
+
+        /**
+         * Add a new field
+         *
+         * @param {String} field The field name
+         * @param {Object} options The validator rules
+         * @returns {BootstrapValidator}
+         */
+        addField: function(field, options) {
+            if (!this.options.fields[field]) {
+                this.options.fields[field] = options;
                 this._initField(field);
             }
 
@@ -1025,6 +1045,24 @@
         },
 
         /**
+         * Remove a given field
+         *
+         * @param {String} field The field name
+         * @returns {BootstrapValidator}
+         */
+        removeField: function(field) {
+            var fields = this.getFieldElements(field),
+                type   = fields.attr('type'),
+                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+
+            for (var i = 0; i < n; i++) {
+                this.removeFieldElement($(fields[i]));
+            }
+
+            return this;
+        },
+
+        /**
          * Reset the form
          *
          * @param {Boolean} resetFormData Reset current form data

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


+ 39 - 33
src/js/bootstrapValidator.js

@@ -1016,7 +1016,27 @@
                 this.$invalidFields.splice(index, 1);
             }
 
-            if ('checkbox' == type || 'radio' == type) {
+            if (this._cacheFields[field].length == 0) {
+                // There is no field with the same name
+                delete this.options.fields[field];
+                delete this._cacheFields[field];
+            } else if ('checkbox' == type || 'radio' == type) {
+                this._initField(field);
+            }
+
+            return this;
+        },
+
+        /**
+         * Add a new field
+         *
+         * @param {String} field The field name
+         * @param {Object} options The validator rules
+         * @returns {BootstrapValidator}
+         */
+        addField: function(field, options) {
+            if (!this.options.fields[field]) {
+                this.options.fields[field] = options;
                 this._initField(field);
             }
 
@@ -1024,6 +1044,24 @@
         },
 
         /**
+         * Remove a given field
+         *
+         * @param {String} field The field name
+         * @returns {BootstrapValidator}
+         */
+        removeField: function(field) {
+            var fields = this.getFieldElements(field),
+                type   = fields.attr('type'),
+                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+
+            for (var i = 0; i < n; i++) {
+                this.removeFieldElement($(fields[i]));
+            }
+
+            return this;
+        },
+
+        /**
          * Reset the form
          *
          * @param {Boolean} resetFormData Reset current form data
@@ -1129,38 +1167,6 @@
                 .removeData('bootstrapValidator')
                 // Remove generated hidden elements
                 .find('[data-bv-submit-hidden]').remove();
-        },
-        
-         /**
-         * Add a new field validation
-         *
-         * @param {String} field The field name
-         * @param {Object} Map the field name with validator rules
-         * 
-         * @return {BootstrapValidator}
-         */
-        addField: function(field, properties) {
-
-            this.options.fields[field] = properties;                    	           
-            this._initField(field);
-            
-            return this;
-                    	            
-        },
-        
-        /**
-         * Remove a field validation
-         *
-         * @param {String} field The field name 
-         * 
-         * @return {BootstrapValidator}
-         */
-        removeField: function(field) {
-
-        	delete this.options.fields[field];
-            
-            return this;
-                    	            
         }
     };