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+
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.
Usage
angular.module('yourapp', [
'AsyncValidator'
])
.config(['AsyncValidatorProvider', function(AsyncValidatorProvider){
AsyncValidatorProvider
.register('name', ['SomeHttpService', function(SomeHttpService){
return function(model, options){
return SomeHttpService.check(model.$modelValue).then(function(returnedFromServer){
if (returnedFromServer.status === 'ok') {
return true;
}
return false;
});
}
}])
.register('required', [function(){
return function(value, options){
return angular.isDefined(value);
};
}], { valueFrom: '$$rawModelValue' })
.register('usingValidateJs', [function(){
return function(value, options){
if (options.someExtraOptions) {
console.log('extra options');
}
return validate.single(value, {
presence: true,
length: {
minimum: 5
},
format: /1910-100/
});
};
}], { valueFrom: '$viewValue', options: { someExtraOptions: true} })
register('equals', function(){
return function(value, options) {
if (!angular.isDefined(options.to)) {
return false;
}
return angular.equals(value.$modelValue, options.to);
};
})
;
}])
.controller('Ctrl', ['AsyncValidator', function(AsyncValidator){
AsyncValidator.run('name', 'Validate this string', { inlineOptions: true }).then(function(currentValidValue){
currentValidValue === 'Validate this string'
}, function(){
});
this.controllerValidation = function($value){
return $value === 'asdf';
};
this.data = {
n1: 'asdf',
n2: '1234',
n3: 'fsa',
n4: 'fda',
n5: 'dsa',
n6: 'ds',
n7: 'dsaa',
value: '2',
ok: 'ok'
};
this.hasChanged = false;
}]);
Use it in your HTML input ng-models (notice they are all expressions, therefore need to be a string):
<div ng-controller="Ctrl as ctrl">
<input
async-validator="{ required: 'required' }"
async-validator-options="{ inline: true }"
ng-model="ctrl.data.n1"
type="text"
>
<input
async-validator="'$model.$modelValue.length > 3'"
async-validator-options-validator="{ outline: true }"
ng-model="ctrl.data.n2"
type="text"
>
<input
async-validator="['strongpassword','length']"
ng-model="ctrl.data.n3"
type="text"
>
<input
async-validator="'equals'"
async-validator-options-equals="{ to: ctrl.data.n3 }"
async-validator-watch="ctrl.data.n3"
ng-model="ctrl.data.n4"
type="text"
>
<input
async-validator="'nome'"
async-validator-options-nome="{ forNome: 'ok' }"
ng-model="ctrl.data.n5"
type="text"
>
<input
async-validator="{ custom: 'ctrl.controllerValidation($value)' }"
ng-model="ctrl.data.n6"
type="text"
>
<input
async-validator="{ inline: '$value != ctrl.data.ok && !$error.required' }"
required
ng-model="ctrl.data.n7"
type="text"
>
</div>
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.
<input async-validator-watch="'ctrl.hasChanged'" async-validator="'$model.$viewValue != ctrl.data.value'" ng-model="data.n6" type="text">
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:
<input
async-validator="['required','specific']"
async-validator-options="{lol: 'yes', ok: true}"
async-validator-options-specific="{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 merged async-validation-options-*
Use it in your form once and apply the same validation to all underlaying models (must name your inputs or manually add them using async-validator-add
):
<form async-validator-form="{ required: 'required', dummy: 'ctrl.controllerValidation($value)' }">
<input type="email" name="email" ng-model"ctrl.data.email">
<input type="tel" ng-model"ctrl.data.phone" async-validator-add>
</form>
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.
valueFrom
where to get the current value. Defaults to undefined
, and passes the whole ngModelController to the validator function as the first parameteroptions
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
will not remove synchronous validators if they have the same name, defaults to false
(removes validators with same name)silentRejection
if sets to false, will rethrow the error. will turn any throws and rejections into an "invalid" validation, defaults to true.
License
MIT