With Angular forms, it's easy to see if a form is $valid or $invalid. This is great and I only allow a user to submit the form if it is $valid.
But, is there an easy way to indicate that it is in the process of validating? (and we're not clear if it's valid or invalid)
This is desirable if you are checking (e.g.) that an email address is valid. I don't want to mark the field as being $invalid until the AJAX call has returned (otherwise I could be showing an error to the user when there isn't one). But I don't want them to be able to submit if we're still checking the email address...
Any suggestions?
The code I'm using for my AJAX validators is based on AngularJS: integrating with server-side validation (thanks to @BenLesh).
function linkFunc(scope, el, attr, ctrl) {
// When the scope changes, check the email.
scope.$watch(attr.ngModel, validate);
function validate (value)
{
// Show as valid until proven invalid
ctrl.$setValidity('validateEmail', true);
// if there was a previous attempt, stop it.
if(timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(function() {
// Make $http call and $setValidity
$http.get(url)
.then(function(data) {
ctrl.$setValidity('validateEmail', data.isValid);
});
}, 200);
}
}