Agent Zebra Agent Zebra - 4 months ago 33
AngularJS Question

Problems with password matching with Angular Auto Validate

Trying to integrate Angular Auto Validate's password matching/confirmation example into a form. I'm not getting any errors but the password matching code is not kicking in. What's the simplest way to fix this? What am I doing wrong?

Here's a plunkr of my code

Here's the password matching code I'm trying to integrate:

function ConfirmPasswordValidatorDirective(defaultErrorMessageResolver) {
defaultErrorMessageResolver.getErrorMessages().then(function (errorMessages) {
errorMessages['confirmPassword'] = 'Please ensure the passwords match.';
});

return {
restrict : 'A',
require : 'ngModel',
scope : {
confirmPassword : '=confirmPassword'
},
link : function(scope, element, attributes, ngModel) {
ngModel.$validators.confirmPassword = function(modelValue) {
return modelValue === scope.confirmPassword;
};

scope.$watch('confirmPassword', function() {
ngModel.$validate();
});
}
};
}

ConfirmPasswordValidatorDirective.$inject = [
'defaultErrorMessageResolver'
];


Here's a plunkr of angular-auto-validate's password matching code working.

Answer

Well, at first you forgot to call the directive in your view, so you should include it in your confirmPassword <input>:

confirm-password="formModel.password"

Also you haven't declared the directive in your JS file:

app.directive('confirmPassword', ConfirmPasswordValidatorDirective);

Look: DEMO

Comments