Directives and service for reusable async validators, write once, use everywhere.
1 person uses it
Author: 461084?v=3 pocesar

Build Status Coverage Status


Angular Async Validator

This module enables you to register your own validation rules, or overwrite existing ones. Makes every validation 'promise based', so it can deal with both synchronous and asynchronous validations. Also, sometimes you want validate an entire form when a model changes, which currently there are no good ways to do this, hence this module, because validation and form manipulation in Angular 1.x is a pain by itself.

Provides no validation functions out-of-the-box. You may reuse the ones from Angular without a problem.

Code was based off ui-validate initially, but it's too simple and lagging behind still using $parsers and $formatters since it need to retain 1.2 compatibility.

This module requires Angular 1.3+, and has no dependencies other than Angular itself.

It also supports 3rd party promise libraries such as RSVP, Q, Bluebird, etc.



Current module implementations only deal with sync validations, validators set in scopes or controllers, or provide 1 directive for each type of validation (validate-number, validate-presence, validate-stuff, etc), which is an overkill.

Async should be norm, and regardless if the validation itself isn't asynchronous, because the UI is asynchronous afterall. Plus there are a plethora of quality validation Javascript libraries, having to rely on Angular built-in ones is too limited, or you having to write a directive for each validation you need is also overkill.

Main goal is to be able with few reusable directives to rule them all, plus 1 service and 1 provider in a concise module that does it's job well without all the bells and whistles.





Use it in your HTML input ng-models (notice they are all expressions, therefore need to be a string):

The helper attribute async-validator-watch can watch an expression. If it changes (regardless if truthy or falsy) will trigger the $validate() call on the ngModel.

For your own options that apply to all validators, use async-validator-options="{}". If you need to specify specifically for one validator write it as async-validator-options-REGISTEREDNAME="{}". Scope and controller variables can be referenced in the options.

The options goes to the least specific and get merged as it becomes more specific. For example:

Locals available:

  • $value current $modelValue, might be undefined / NaN
  • $error current $error in the underlaying ng-model
  • $model current ng-model exposed
  • $options current merged async-validation-options-*

async-form-validator and async-group-validator can apply validations and options to the children ngModels.

For async-form-validator, every named ngModel will be automatically added. If you want to exclude one model, add the async-validator-exclude to the element. You can also add non-named ngModels using async-validator-add.

For async-group-validator, you can use common validators for a group of ngModels, but they don't add themselves automatically like async-form-validator does, you need to manually add them using async-validator-add. async-group-validator has precedence over a async-form-validator, so you can overwrite a group inside a form.

Options are also merged from top to bottom (async-form-validator > async-group-validator > async-validator-options > async-validator-options-validator)


When registering a validator, you can pass your own options to it using the third parameter as an object and setting the options member.

  • options any options that the validator function receives as the second parameter, defaults to {}

  • overwrite if you set to false, it will throw if there's another validator with same name, defaults to true

  • removeSync removes synchronous validators if they have the same name as your registered validator, defaults to true. Eg: using <input ng-model="model" required async-validator="'required'"> will delete the default required validator

  • silentRejection if sets to false, will rethrow the error. will turn any throws and rejections into an "invalid" validation, defaults to true.



comments powered by Disqus
This page was last updated almost 2 years ago.