0

See the example here.

Using the $validators pipeline, I am trying to check that a field contains the same value as another field.

Each input in this example is associated with the other, such that the expected result is as follows:

  • Enter a value in input#1
  • Enter same value in input#2
  • Both fields should now be valid
  • Alter the value in input#1
  • input#1 should be invalid (or input#2 or both)

Initially, I did this using a $watch on both the current model and the target to be equal to, so only one of the two fields needed to use the directive. However, with the introduction of the $validators pipeline, this method stopped working unexpectedly (maybe a bug).

Anyhow, as you can see, when the second input is altered, the value is receives for the associated input is undefined.

Solution

I solved this by the following:

JSFiddle

As Nikos said, the two instances were cancelling each other out, so this was fixed by the following code:

$scope.$watch('passwordWatch', function(pass) {
    $control.$validate();
});

So now, when the target input changes, the current input revalidates. When the current input changes, it validates automatically (as usual).

1 Answer 1

1

One problem is that when a validator fails (returns false), then the underlying model value is set to undefined. So:

  1. You type something in the password, say "aaa"; this is NOT the same as passwordConfirm, so the validator returns false and the model gets the undefined value
  2. You type the same value in passwordConfirm; but from above the value of the password is undefined and undefined !== "aaa", so the passwordConfirm validator returns false too.
  3. And so on...
Sign up to request clarification or add additional context in comments.

3 Comments

haha i completely overlooked that. the documentation on the $validators pipeline is lacking currently so i was unaware it did that. any suggestion as to how this should be done then?
:) Actually I tried tweaking the fiddle, but it seems a bit more complex than just tweaking or I am doing something wrong. Hopefully I will get back to this later. What I am trying to do is use the classic $parsers/$formatters instead of the $validators. The $parsers/$formatters allow you to return a value, even if the input is invalid, so the model can still (?) be updated and then the next check potentially succeed.
check out my edit in the main question. i got it working currently by only listening on one of the two inputs and revalidating when the target changes.

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.