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.
Motivation
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.
Install
NPM
$ npm instal angular-async-validator --save
Bower
$ bower install angular-async-validator --save
Usage
angular// Configure your validators// reuse validation programatically;
Use it in your HTML input ng-models (notice they are all expressions, therefore need to be a string):
<!-- can mix synchronous angular validations with async, in this case, using the "required" -->
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:
<!-- required validator will receive the {lol: 'yes', ok: true} --> <!-- specific validator will receive the {lol: 'yes', ok: false} -->
Locals available:
$value
current$modelValue
, might be undefined / NaN$error
current$error
in the underlaying ng-model$model
current ng-model exposed$options
current mergedasync-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
)
<!-- value will have to pass Angular internal required and our registered dummy validator --> <!-- value will have to pass Angular internal required and our registered dummy validator --> <!-- apply the same validator to the models in the group --> <!-- async-validator-exclude will exclude the parent controller to add the validators to it -->
Options
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 totrue
-
removeSync
removes synchronous validators if they have the same name as your registered validator, defaults totrue
. Eg: using<input ng-model="model" required async-validator="'required'">
will delete the defaultrequired
validator -
silentRejection
if sets to false, will rethrow the error. will turn any throws and rejections into an "invalid" validation, defaults to true.
License
MIT