TheSharpieOne/angular-validation-match

match validation is incorrect if source field is invalid

Closed this issue · 4 comments

Say you have a password field with some validator on it, such as requiring it to be at least 8 chars long. Then you have a password2 confirmation field which uses the match directive to make sure it matches the password field. If one enters the same value on both fields, but the value does not pass the password field's validation (e.g. only 2 chars are entered), then password2 is incorrectly marked as invalid even though it does match the password field.

The match directive should only mark the field as invalid if its value is not identical to the source field's value, regardless of whether the source field has other validation issues. The current behavior causes incorrect error messages to be displayed to the user, e.g. a "passwords do not match" error is shown in addition to an 'invalid password length' error, whereas only the latter is true, and once the user fixes it, only then should the second message be shown.

The problems stems from the first validation making the internal value of the invalid fields as undefined. This is in angular's code. Since the value entered was not valid it becomes undefined and undefined !== password2.

I believe we may be able to get the $viewValue of the input and compare to that instead of the $modelValue that is on scope.match. Not sure how this will work in Chrome with type=number as it does some funky things when you enter non-number values.

Also, is this 1.2.x or 1.3.x ?

I'm on 1.3.2.

Didn't you tackle this already here?

That just got it working in 1.3.x with the new way the $modelView works with invalid fields. But that is for the element that the match attribute is on. The element that is it matching scope.match is still looking for the $modelView.

The 1.3.x solution would be to be to get the $invalidModelView of the other field.
1.2.x solution would be to get the $viewValue of the other field (since 1.2.x doesn't have $invalidModelView and typically just sets $modelView to undefined when invalid).

I'll get this fixed when I get a chance.

A new ability was added, see the README section about "Validate Against the $viewValue shown in the input"