0

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.

5
  • There are a lot of posts / ready directives to do this, look this for example: stackoverflow.com/questions/14012239/… Commented Jul 13, 2016 at 23:33
  • Thank you, I don't feel that addresses the specific issues in the above post using Angular Auto Validate. Commented Jul 13, 2016 at 23:36
  • True, I didn't notice you're using a module since you didn't put it specifically. Commented Jul 13, 2016 at 23:38
  • :) Thanks. We were both editing that info at the same time. Commented Jul 13, 2016 at 23:43
  • 1
    I find your error, check my answer :) Commented Jul 13, 2016 at 23:59

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you fantastic. Ah " see, scope : { confirmPassword : '=confirmPassword' }, was not reflected in the confirmPassword <input> Just one question so I can understand: why did you remove class="control-label" on the password labels?
Hmm, maybe, I copied the fields of their plnkr :), but you can just do these modifications above on your plnkr and it will works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.