Browse Source

#275: Add destroy() method

nghuuphuoc 11 years ago
parent
commit
aa01b9e871
4 changed files with 123 additions and 4 deletions
  1. 1 0
      CHANGELOG.md
  2. 60 1
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 60 1
      src/js/bootstrapValidator.js

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 * [#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
 * [#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
 * [#274](https://github.com/nghuuphuoc/bootstrapvalidator/pull/274): Fix feedback icons in ```input-group```, thanks to [@tiagofontella](https://github.com/tiagofontella)
 
 ## v0.4.5 (2015-05-15)

+ 60 - 1
dist/js/bootstrapValidator.js

@@ -180,7 +180,7 @@
                     e.preventDefault();
                     that.validate();
                 })
-                .on('click', this.options.submitButtons, function() {
+                .on('click.bv', this.options.submitButtons, function() {
                     that.$submitButton  = $(this);
 					// The user just click the submit button
 					that._submitIfValid = true;
@@ -243,6 +243,7 @@
                     .each(function() {
                         $('<input/>')
                             .attr('type', 'hidden')
+                            .attr('data-bv-submit-hidden', '')
                             .attr('name', $(this).attr('name'))
                             .val($(this).val())
                             .appendTo(that.$form);
@@ -1114,6 +1115,64 @@
             this.updateStatus(field, this.STATUS_NOT_VALIDATED);
 
             return this;
+        },
+
+        /**
+         * Destroy the plugin
+         * It will remove all error messages, feedback icons and turn off the events
+         */
+        destroy: function() {
+            var field, fields, $field, validator, $icon, container;
+            for (field in this.options.fields) {
+                fields    = this.getFieldElements(field);
+                container = this.options.fields[field].container || this.options.container;
+                for (var i = 0; i < fields.length; i++) {
+                    $field = $(fields[i]);
+                    $field
+                        // Remove all error messages
+                        .data('bv.messages')
+                            .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove().end()
+                            .end()
+                        .removeData('bv.messages')
+                        // Remove feedback classes
+                        .parents('.form-group')
+                            .removeClass('has-feedback has-error has-success')
+                            .end()
+                        // Turn off events
+                        .off('.bv')
+                        .removeAttr('data-bv-field');
+
+                    // Remove feedback icons, tooltip/popover container
+                    $icon = $field.parents('.form-group').find('i[data-bv-icon-for="' + field + '"]');
+                    if ($icon) {
+                        switch (container) {
+                            case 'tooltip':
+                                $icon.tooltip('destroy').remove();
+                                break;
+                            case 'popover':
+                                $icon.popover('destroy').remove();
+                                break;
+                            default:
+                                $icon.remove();
+                                break;
+                        }
+                    }
+
+                    for (validator in this.options.fields[field].validators) {
+                        $field.removeData('bv.result.' + validator).removeData('bv.dfs.' + validator);
+                    }
+                }
+            }
+
+            // Enable submit buttons
+            this.disableSubmitButtons(false);
+
+            this.$form
+                .removeClass(this.options.elementClass)
+                .off('.bv')
+                .removeData('bootstrapValidator')
+                // Remove generated hidden elements
+                .find('[data-bv-submit-hidden]').remove();
         }
     };
 

File diff suppressed because it is too large
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 60 - 1
src/js/bootstrapValidator.js

@@ -179,7 +179,7 @@
                     e.preventDefault();
                     that.validate();
                 })
-                .on('click', this.options.submitButtons, function() {
+                .on('click.bv', this.options.submitButtons, function() {
                     that.$submitButton  = $(this);
 					// The user just click the submit button
 					that._submitIfValid = true;
@@ -242,6 +242,7 @@
                     .each(function() {
                         $('<input/>')
                             .attr('type', 'hidden')
+                            .attr('data-bv-submit-hidden', '')
                             .attr('name', $(this).attr('name'))
                             .val($(this).val())
                             .appendTo(that.$form);
@@ -1113,6 +1114,64 @@
             this.updateStatus(field, this.STATUS_NOT_VALIDATED);
 
             return this;
+        },
+
+        /**
+         * Destroy the plugin
+         * It will remove all error messages, feedback icons and turn off the events
+         */
+        destroy: function() {
+            var field, fields, $field, validator, $icon, container;
+            for (field in this.options.fields) {
+                fields    = this.getFieldElements(field);
+                container = this.options.fields[field].container || this.options.container;
+                for (var i = 0; i < fields.length; i++) {
+                    $field = $(fields[i]);
+                    $field
+                        // Remove all error messages
+                        .data('bv.messages')
+                            .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove().end()
+                            .end()
+                        .removeData('bv.messages')
+                        // Remove feedback classes
+                        .parents('.form-group')
+                            .removeClass('has-feedback has-error has-success')
+                            .end()
+                        // Turn off events
+                        .off('.bv')
+                        .removeAttr('data-bv-field');
+
+                    // Remove feedback icons, tooltip/popover container
+                    $icon = $field.parents('.form-group').find('i[data-bv-icon-for="' + field + '"]');
+                    if ($icon) {
+                        switch (container) {
+                            case 'tooltip':
+                                $icon.tooltip('destroy').remove();
+                                break;
+                            case 'popover':
+                                $icon.popover('destroy').remove();
+                                break;
+                            default:
+                                $icon.remove();
+                                break;
+                        }
+                    }
+
+                    for (validator in this.options.fields[field].validators) {
+                        $field.removeData('bv.result.' + validator).removeData('bv.dfs.' + validator);
+                    }
+                }
+            }
+
+            // Enable submit buttons
+            this.disableSubmitButtons(false);
+
+            this.$form
+                .removeClass(this.options.elementClass)
+                .off('.bv')
+                .removeData('bootstrapValidator')
+                // Remove generated hidden elements
+                .find('[data-bv-submit-hidden]').remove();
         }
     };