Skip to content
This repository was archived by the owner on Feb 2, 2019. It is now read-only.

Commit 74e19cf

Browse files
author
codekraft-studio
committed
Features: multiple validator callbacks as object
1 parent 080a0a8 commit 74e19cf

5 files changed

+415
-247
lines changed

README.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ Now you are able to use the directive: __async-validation__ in your app.
2222
### How it works?
2323
This directive is designed to be reused several times depending on the purpose.
2424
The directive can be used with other validation directives
25-
There are two main ways to use it, the first is to use a string that points to the API to which you want to make the asynchronous request, the other is using a user-specified function.
25+
There are many ways to use it, the first is to use a string that points to the API to which you want to make the asynchronous request, another is using a user-specified function, than you can pass an object of funtions where the key is the validator name and the value is the validation callback.
2626
```html
27+
2728
<input ng-model="myInput" name="" async-validation="'/api/users/:value'" required />
29+
2830
<input ng-model="myInput" name="" async-validation="myFunction" required />
31+
32+
<input ng-model="myInput" name="" async-validation="{
33+
firstvalidation: firstFunction,
34+
secondvalidation: secondFunction
35+
}" required />
36+
2937
```
3038

3139
When you use a string that point to a API service, like in the first example, remember that:
@@ -39,7 +47,11 @@ For more info see the [NgModelController](https://docs.angularjs.org/api/ng/type
3947
---
4048

4149
### Examples
42-
Using the directive with a custom validation function specified in the controller.
50+
51+
#### Using a function:
52+
53+
You can use a function from the scope of your controller.
54+
4355
```html
4456
<input ng-model="myInput" name="fullname" async-validation="myValidation" />
4557
```
@@ -69,7 +81,10 @@ $scope.myValidation = function (modelValue, viewValue) {
6981
}
7082
```
7183

72-
Using a string that point to a API service.
84+
#### Using a string that point to a API service:
85+
86+
You can pass directly a string that is the endpoint for your validation API.
87+
7388
```html
7489
<!-- for validate username -->
7590
<input ng-model="user.username" name="username" async-validation="'/api/users/username/:value'" required />
@@ -82,3 +97,15 @@ If typed "__test__" will produce: `GET /api/users/username/test`.
8297
If typed "__[email protected]__" will produce: `GET /api/users/email/[email protected]`.
8398

8499
Than probably you want to check for the existence of that particular username/email in case of new registration to avoid duplicates or conflicts.
100+
101+
#### Creating multiple async validators:
102+
103+
If you want to run your input validation against multiple async functions you can pass an object like in this example.
104+
105+
```html
106+
<!-- will validate against firstFunction and secondFunction -->
107+
<input ng-model="myInput" name="" async-validation="{
108+
firstvalidation: firstFunction,
109+
secondvalidation: secondFunction
110+
}" required />
111+
```

dist/angular-async-validation.js

+93-54
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/**
2-
* Package: angular-async-validation - v0.0.4
2+
* Package: angular-async-validation - v0.0.5
33
* Description: A multi purpose directive for input async validation
4-
* Last build: 2017-05-04
4+
* Last build: 2017-06-23
55
* @author codekraft-studio
66
* @license ISC
77
*/
88
angular.module('angular-async-validation', []);
99

1010
angular.module('angular-async-validation')
1111

12-
.directive('asyncValidation', ['$log', '$q', '$http', function ($log, $q, $http) {
12+
.directive('asyncValidation', ['$log', '$q', '$http', '$parse', function ($log, $q, $http, $parse) {
13+
14+
var _pattern = new RegExp(':value');
1315

1416
var directive = {
1517
restrict: 'A',
@@ -19,18 +21,18 @@ angular.module('angular-async-validation')
1921
};
2022

2123
return directive;
22-
24+
2325
function _link(scope, elem, attrs, ngModel) {
24-
26+
2527
// The async validation function
2628
var validationFunction, validatorName = scope.validatorName ? scope.validatorName : 'asyncValidator';
2729

2830
// Check if the argument passed satisfy the requirements
29-
if( !scope.asyncValidation && !angular.isString(scope.asyncValidation) && !angular.isFunction(scope.asyncValidation) ) {
31+
if( !scope.asyncValidation ) {
3032
$log.warn('angular-async-validation: missing or empty argument in async-validation attribute on:', elem);
3133
return;
3234
}
33-
35+
3436
// If no options are specified set to the defaults
3537
if( !ngModel.$options || !ngModel.$options.getOption('debounce') ) {
3638

@@ -42,56 +44,93 @@ angular.module('angular-async-validation')
4244

4345
}
4446

45-
// If is a string use it as path for http request
46-
if( angular.isString(scope.asyncValidation) ) {
47-
48-
validationFunction = function (modelValue, viewValue) {
49-
50-
// get the current value
51-
var value = modelValue || viewValue;
52-
53-
// Consider empty models to be valid
54-
// for this type of validation
55-
if (ngModel.$isEmpty(value)) {
56-
return $q.resolve();
57-
}
58-
59-
// Init the deferred object
60-
var deferred = $q.defer();
61-
62-
// build the url
63-
var url = scope.asyncValidation.replace(':value', value);
64-
65-
// run the request
66-
$http.get(url, {
67-
notifyError: false
68-
}).then(function (response) {
69-
70-
if( !response.data ) {
71-
deferred.resolve();
72-
} else {
73-
deferred.reject();
74-
}
47+
// Example: async-validation="{ validator1: myFunc, validator2: myFunc2 }"
48+
//
49+
// If is a object of validator fucntions
50+
if( angular.isObject(scope.asyncValidation) ) {
51+
52+
angular.forEach(scope.asyncValidation, function(callback, validator) {
53+
54+
// TODO: chck if is passed a func or string
55+
56+
if( angular.isFunction(callback) ) {
7557

76-
}, function () {
77-
deferred.resolve();
78-
});
79-
80-
return deferred.promise;
81-
82-
};
83-
58+
// Add the async validator using custom validator name
59+
ngModel.$asyncValidators[validator] = callback;
60+
61+
}
62+
63+
});
64+
8465
}
85-
86-
// If is a function defined by users
87-
// assign it to asyncValidators
88-
if( angular.isFunction(scope.asyncValidation) ) {
89-
validationFunction = scope.asyncValidation;
66+
67+
// Check scope variable type
68+
if( angular.isString(scope.asyncValidation) || angular.isFunction(scope.asyncValidation) ) {
69+
70+
// Example: async-validation="myFunc"
71+
//
72+
// If is a valid function assign it directly
73+
if( angular.isFunction(scope.asyncValidation) ) {
74+
validationFunction = scope.asyncValidation;
75+
}
76+
77+
// Example:
78+
// async-validation="'/api/test/:value'"
79+
// async-validation="'/api/test?q=:value'"
80+
//
81+
// If is a valid formatted string use it in a custom function
82+
if( angular.isString(scope.asyncValidation) ) {
83+
84+
// Inform user that bad string was used
85+
if( !_pattern.test(scope.asyncValidation) ) {
86+
$log.warn('angular-async-validation: You enter a bad callback string:', scope.asyncValidation, 'it must contain :value');
87+
return;
88+
}
89+
90+
// Create a custom validation callback
91+
validationFunction = function (modelValue, viewValue) {
92+
93+
// get the current value
94+
var value = modelValue || viewValue;
95+
96+
// Consider empty models to be valid
97+
// for this type of validation
98+
if (ngModel.$isEmpty(value)) {
99+
return $q.resolve();
100+
}
101+
102+
// Init the deferred object
103+
var deferred = $q.defer();
104+
105+
// build the url
106+
var url = scope.asyncValidation.replace(':value', value);
107+
108+
// run the request
109+
$http.get(url, {
110+
notifyError: false
111+
}).then(function (response) {
112+
113+
if( !response.data ) {
114+
deferred.resolve();
115+
} else {
116+
deferred.reject();
117+
}
118+
119+
}, function () {
120+
deferred.resolve();
121+
});
122+
123+
return deferred.promise;
124+
125+
};
126+
127+
}
128+
129+
// Add the async validator (optionally using custom name)
130+
ngModel.$asyncValidators[validatorName] = validationFunction;
131+
90132
}
91-
92-
// Add the async validator (optionally using custom name)
93-
ngModel.$asyncValidators[validatorName] = validationFunction;
94-
133+
95134
}
96135

97136
}]);

dist/angular-async-validation.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)