MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Async validators


Asynchronous validators allows you to validate form information against your backend (using $http).

These kind of validators are needed when you need to access server stored information you can't have on your client for various reasons, such as the users table and other database information.

To use async validators, you access the ng-model of your input and define callback functions for the $asyncValidators property.

Example:


The following example checks if a provided name already exists, the backend will return a status that will reject the promise if the name already exists or if it wasn't provided. If the name doesn't exist it will return a resolved promise.

ngModel.$asyncValidators.usernameValidate = function (name) { 
 if (name) {
 return AuthenticationService.checkIfNameExists(name); // returns a promise
 } else {
 return $q.reject("This username is already taken!"); // rejected promise
 }
};

Now every time the ng-model of the input is changed, this function will run and return a promise with the result

Conclusion

In this page (written and validated by ) you learned about AngularJS Async validators . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Routing using ngRoute.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.