Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Validator.VALIDATORS leak #176

Closed
Spown opened this issue Aug 6, 2015 · 0 comments
Closed

Validator.VALIDATORS leak #176

Spown opened this issue Aug 6, 2015 · 0 comments

Comments

@Spown
Copy link

Spown commented Aug 6, 2015

the way custom validators are stored results in sharing this option across multiple forms. For example if i have auth and reg forms on the same page and want to apply different validators to them - the fileds with the same names trigger the same validators on both forms. It's not a big deal, since only the accessed element gets the responce, but still the request is still made more than once, which is not exactly fine.

The problem is that you made this variable static js-way.

Here is my fix, line numbers do not match since I've already moded the plugin prior this patch.


---
 main/static/js/bootstrap.validator.js | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/main/static/js/bootstrap.validator.js b/main/static/js/bootstrap.validator.js
index ce84bb8..73c534c 100644
--- a/main/static/js/bootstrap.validator.js
+++ b/main/static/js/bootstrap.validator.js
@@ -34,16 +34,16 @@
   // ==========================

   var Validator = function (element, options) {
-    this.$element = $(element)
-    this.options  = options
+    this.$element = $(element);
+    this.options  = $.extend(true, {}, options);

-    options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors)
+    //this.options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors)

     for (var custom in options.custom) {
-      if (!options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom)
+      if (!this.options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom);
     }

-    $.extend(Validator.VALIDATORS, options.custom)
+    this.VALIDATORS = $.extend(true, {}, Validator.DEFAULT_VALIDATORS, options.custom);

     this.$element.attr('novalidate', true) // disable automatic native validation
     this.toggleSubmit()
@@ -76,7 +76,7 @@
     }
   }

-  Validator.VALIDATORS = {
+  Validator.DEFAULT_VALIDATORS = {
     'native': function ($el) {
       var el = $el[0]
       return el.checkValidity ? el.checkValidity() : true
@@ -140,7 +140,7 @@
         || options.errors[key]
     }

-    $.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
+    $.each(this.VALIDATORS, $.proxy(function (key, validator) {
       var df = $.Deferred(), valRet = true
       ;
       try { valRet = validator.call(this, $el) }
-- 
1.9.5.msysgit.0


@1000hz 1000hz closed this as completed in 37ea083 Jul 15, 2016
andrews05 pushed a commit to andrews05/bootstrap-validator that referenced this issue Mar 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant