My form in Angular needs to be able to detect duplicate user emails.
The code I'm concerned about can be found here: http://plnkr.co/edit/XQeFHJTsgZONbsrxnvcI
This code includes the following directives:
ngFocus
: tracks whether an input is on focus or blur, and uses a variablehasVisited
to only validate the input on blur if it has been reached by the user at least once.emailUnique
: compares the email entered with an existing hardcoded email to determine if it's valid.
To see the field validation error, make sure to press the "tab" key to trigger the blur
function.
- If I leave the email input empty and move to the Name input (to blur the email input), it shows: The field is required. That's working as expected.
- If I enter an invalid email like "ferfergre", it displays: This email isn't valid.. That's also working correctly.
- If I then enter a valid email different from the hardcoded one: ([email protected]), no error is displayed which is correct.
- If I enter the existing email: [email protected], it shows: This email already exists.. This warning is necessary.
- However, if an email has been flagged as existing during the DOM cycle, even if I change it to a new valid one, the error remains...
Question: How can I handle this last case?
I have two hypotheses:
- The order of HTML attributes might play a role:
autofocus email-unique ng-focus required
- The
setValidity
method in theemailUnique
directive could preventctrl
from being$valid
again, thus stopping the email comparison from running again.