Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit fda90d0

Browse files
committed
Fixed issue #68 - Matching Validation confirmation
- Added a $watch on the parent so that it will invalidate confirmation field as soon as parent is touched.
1 parent c72a2f0 commit fda90d0

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-validation-ghiscoding",
3-
"version": "1.4.7",
3+
"version": "1.4.8",
44
"author": "Ghislain B.",
55
"description": "Angular-Validation Directive and Service (ghiscoding)",
66
"main": [

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Angular-Validation change logs
22

3+
1.4.8 (2015-09-12) Fixed issue #68 - Matching validation issue (password confirmation).
34
1.4.7 (2015-09-08) Fixed issue #65 - invalid return value on function String.format()
45
1.4.6 (2015-09-04) Accepted pull #64 - Update Spanish `es.json` translation file.
56
1.4.5 (2015-09-03) Fixed issue #63 - Custom Regex pattern should be greedy (instead of non-greedy), this was causing a problem when user add a `/` in his pattern and was stopping on first occurence.

dist/angular-validation.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-validation-ghiscoding",
3-
"version": "1.4.7",
3+
"version": "1.4.8",
44
"author": "Ghislain B.",
55
"description": "Angular-Validation Directive and Service (ghiscoding)",
66
"main": "app.js",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Angular Validation (Directive / Service)
2-
`Version: 1.4.7`
2+
`Version: 1.4.8`
33
### Form validation after user inactivity of default 1sec. (customizable timeout)
44

55
Forms Validation with Angular made easy! Angular-Validation is an angular directive/service with locales (languages) with a very simple approach of defining your `validation=""` directly within your element to validate (input, textarea, etc) and...that's it!!! The directive/service will take care of the rest!

src/validation-common.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,21 +489,50 @@ angular
489489
// it might be a match input checking
490490
else if (validator.type === "matching") {
491491
// get the element 'value' ngModel to compare to (passed as params[0], via an $eval('ng-model="modelToCompareName"')
492-
var otherNgModel = validator.params[0];
493-
var otherNgModelVal = self.scope.$eval(otherNgModel);
494-
var elm = angular.element(document.querySelector('[name="'+otherNgModel+'"]'));
492+
// for code purpose we'll name the other parent element "parent..."
493+
// and we will name the current element "matching..."
494+
var parentNgModel = validator.params[0];
495+
var parentNgModelVal = self.scope.$eval(parentNgModel);
496+
var otherElm = angular.element(document.querySelector('[name="'+parentNgModel+'"]'));
497+
var matchingValidator = validator; // keep reference of matching confirmation validator
498+
var matchingCtrl = self.ctrl; // keep reference of matching confirmation controller
499+
var formElmMatchingObj = getFormElementByName(self.ctrl.$name);
495500

496-
isValid = (testCondition(validator.condition, strValue, otherNgModelVal) && !!strValue);
501+
isValid = (testCondition(validator.condition, strValue, parentNgModelVal) && !!strValue);
497502

498503
// if element to compare against has a friendlyName or if matching 2nd argument was passed, we will use that as a new friendlyName
499504
// ex.: <input name='input1' friendly-name='Password1'/> :: we would use the friendlyName of 'Password1' not input1
500505
// or <input name='confirm_pass' validation='match:input1,Password2' /> :: we would use Password2 not input1
501-
if(!!elm && !!elm.attr('friendly-name')) {
502-
validator.params[1] = elm.attr('friendly-name');
506+
if(!!otherElm && !!otherElm.attr('friendly-name')) {
507+
validator.params[1] = otherElm.attr('friendly-name');
503508
}
504509
else if(validator.params.length > 1) {
505510
validator.params[1] = validator.params[1];
506511
}
512+
513+
// Watch for the parent ngModel, if it change we need to re-validate the child (confirmation)
514+
self.scope.$watch(parentNgModel, function(newVal, oldVal) {
515+
var isWatchValid = testCondition(matchingValidator.condition, matchingCtrl.$viewValue, newVal);
516+
517+
// only inspect on a parent input value change
518+
if(newVal !== oldVal) {
519+
// If Valid then erase error message ELSE make matching field Invalid
520+
if(isWatchValid) {
521+
addToValidationAndDisplayError(self, formElmMatchingObj, '', true, true);
522+
}else {
523+
formElmMatchingObj.isValid = false;
524+
var msgToTranslate = matchingValidator.message;
525+
if (!!matchingValidator.altText && matchingValidator.altText.length > 0) {
526+
msgToTranslate = matchingValidator.altText.replace("alt=", "");
527+
}
528+
$translate(msgToTranslate).then(function (translation) {
529+
message = ' ' + ((!!matchingValidator && !!matchingValidator.params) ? String.format(translation, matchingValidator.params) : translation);
530+
addToValidationAndDisplayError(self, formElmMatchingObj, message, isWatchValid, true);
531+
});
532+
}
533+
matchingCtrl.$setValidity('validation', isWatchValid); // change the validity of the matching input
534+
}
535+
}, true); // .$watch()
507536
}
508537
// it might be a remote validation, this should return a promise with the result as a boolean or a { isValid: bool, message: msg }
509538
else if (validator.type === "remote") {
@@ -709,7 +738,7 @@ angular
709738

710739
// error Display
711740
if (showError && !!formElmObj && !formElmObj.isValid) {
712-
self.updateErrorMsg(message, { isValid: isFieldValid });
741+
self.updateErrorMsg(message, { isValid: isFieldValid, obj: formElmObj });
713742
} else if (!!formElmObj && formElmObj.isValid) {
714743
addToValidationSummary(formElmObj, '');
715744
}

0 commit comments

Comments
 (0)