diff --git a/bower.json b/bower.json index ed6be05..d85bf91 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "tg-angular-validator", - "version": "1.2.6", + "version": "1.2.7", "authors": [ "Zohar Jackson " ], diff --git a/demo/index.html b/demo/index.html index e6f00ab..8f27478 100644 --- a/demo/index.html +++ b/demo/index.html @@ -2,21 +2,16 @@ Angular-Validator Demo - - - - +

Angular-Validator Demo

https://github.com/turinggroup/angular-validator
-

Choose when to validate:

diff --git a/dist/angular-validator.js b/dist/angular-validator.js index 67fd05d..caef123 100644 --- a/dist/angular-validator.js +++ b/dist/angular-validator.js @@ -9,6 +9,9 @@ angular.module('angularValidator').directive('angularValidator', // This is the DOM form element var DOMForm = angular.element(element)[0]; + // an array to store all the watches for form elements + var watches = []; + // This is the the scope form model // All validation states are contained here var form_name = DOMForm.attributes['name'].value; @@ -16,10 +19,12 @@ angular.module('angularValidator').directive('angularValidator', // Set the default submitted state to false scopeForm.submitted = false; - // Watch form length to add watches for new form elements - scope.$watch(function(){return DOMForm.length;}, function(){ + scope.$watch(function(){return Object.keys(scopeForm).length;}, function(){ + // Destroy all the watches + // This is cleaner than figuring out which items are already being watched and only un-watching those. + angular.forEach(watches, function(watch){watch();}); setupWatches(DOMForm); }); @@ -48,13 +53,11 @@ angular.module('angularValidator').directive('angularValidator', scopeForm[DOMForm[i].name].$render(); } } - scopeForm.submitted = false; scopeForm.$setPristine(); }; - // Setup watches on all form fields setupWatches(DOMForm); @@ -72,13 +75,6 @@ angular.module('angularValidator').directive('angularValidator', // Setup $watch on a single formfield function setupWatch(elementToWatch) { - - if (elementToWatch.isWatchedByValidator){ - return; - } - - elementToWatch.isWatchedByValidator = true; - // If element is set to validate on blur then update the element on blur if ("validate-on" in elementToWatch.attributes && elementToWatch.attributes["validate-on"].value === "blur") { angular.element(elementToWatch).on('blur', function() { @@ -87,7 +83,7 @@ angular.module('angularValidator').directive('angularValidator', }); } - scope.$watch(function() { + var watch = scope.$watch(function() { return elementToWatch.value + elementToWatch.required + scopeForm.submitted + checkElementValidity(elementToWatch) + getDirtyValue(scopeForm[elementToWatch.name]) + getValidValue(scopeForm[elementToWatch.name]); }, function() { @@ -112,6 +108,8 @@ angular.module('angularValidator').directive('angularValidator', } }); + + watches.push(watch); } diff --git a/dist/angular-validator.min.js b/dist/angular-validator.min.js index ef95326..edb1205 100644 --- a/dist/angular-validator.min.js +++ b/dist/angular-validator.min.js @@ -1 +1 @@ -angular.module("angularValidator",[]),angular.module("angularValidator").directive("angularValidator",function(){return{restrict:"A",link:function(a,b){function c(a){for(var b=0;b Required"},d=function(){return" Invalid"};if(b.name in n){var e=n[b.name],f=j(b);f&&f.remove(),(e.$dirty||a[b.form.name]&&a[b.form.name].submitted)&&(e.$error.required?angular.element(b).after("required-message"in b.attributes?i(b.attributes["required-message"].value):i(c)):e.$valid||angular.element(b).after("invalid-message"in b.attributes?i(b.attributes["invalid-message"].value):i(d)))}}function i(b){return""}function j(a){for(var b=angular.element(a).parent().children(),c=0;c Required"},d=function(){return" Invalid"};if(b.name in o){var e=o[b.name],f=j(b);f&&f.remove(),(e.$dirty||a[b.form.name]&&a[b.form.name].submitted)&&(e.$error.required?angular.element(b).after("required-message"in b.attributes?i(b.attributes["required-message"].value):i(c)):e.$valid||angular.element(b).after("invalid-message"in b.attributes?i(b.attributes["invalid-message"].value):i(d)))}}function i(b){return""}function j(a){for(var b=angular.element(a).parent().children(),c=0;c