Skip to content

Commit

Permalink
Fixed issue with validtor and ng-if
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakobovski committed Jul 12, 2015
1 parent 9b67aa5 commit 226fd52
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tg-angular-validator",
"version": "1.2.6",
"version": "1.2.7",
"authors": [
"Zohar Jackson <[email protected]>"
],
Expand Down
11 changes: 3 additions & 8 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
<head>
<title>Angular-Validator Demo</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style>
h4 {
color: rgb(255, 128, 0);
}
<style>
h4 {color: rgb(255, 128, 0);}
</style>


</head>
</head>

<body ng-controller="DemoCtrl">
<div class="container">
<h1>Angular-Validator Demo</h1>
<a href="https://github.com/turinggroup/angular-validator">https://github.com/turinggroup/angular-validator</a>
<hr>

<form angular-validator-submit="submitMyForm()" name="myForm" class="form-horizontal" novalidate angular-validator>
<h4>Choose when to validate:</h4>
<div class="form-group">
Expand Down
22 changes: 10 additions & 12 deletions dist/angular-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ 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;
var scopeForm = scope[form_name];

// 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);
});

Expand Down Expand Up @@ -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);

Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -112,6 +108,8 @@ angular.module('angularValidator').directive('angularValidator',
}

});

watches.push(watch);
}


Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions src/angular-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ 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;
var scopeForm = scope[form_name];

// 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);
});

Expand Down Expand Up @@ -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);

Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -112,6 +108,8 @@ angular.module('angularValidator').directive('angularValidator',
}

});

watches.push(watch);
}


Expand Down

0 comments on commit 226fd52

Please sign in to comment.