1
1
/**
2
- * Package: angular-async-validation - v0.0.4
2
+ * Package: angular-async-validation - v0.0.5
3
3
* Description: A multi purpose directive for input async validation
4
- * Last build: 2017-05-04
4
+ * Last build: 2017-06-23
5
5
* @author codekraft-studio
6
6
* @license ISC
7
7
*/
8
8
angular . module ( 'angular-async-validation' , [ ] ) ;
9
9
10
10
angular . module ( 'angular-async-validation' )
11
11
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' ) ;
13
15
14
16
var directive = {
15
17
restrict : 'A' ,
@@ -19,18 +21,18 @@ angular.module('angular-async-validation')
19
21
} ;
20
22
21
23
return directive ;
22
-
24
+
23
25
function _link ( scope , elem , attrs , ngModel ) {
24
-
26
+
25
27
// The async validation function
26
28
var validationFunction , validatorName = scope . validatorName ? scope . validatorName : 'asyncValidator' ;
27
29
28
30
// Check if the argument passed satisfy the requirements
29
- if ( ! scope . asyncValidation && ! angular . isString ( scope . asyncValidation ) && ! angular . isFunction ( scope . asyncValidation ) ) {
31
+ if ( ! scope . asyncValidation ) {
30
32
$log . warn ( 'angular-async-validation: missing or empty argument in async-validation attribute on:' , elem ) ;
31
33
return ;
32
34
}
33
-
35
+
34
36
// If no options are specified set to the defaults
35
37
if ( ! ngModel . $options || ! ngModel . $options . getOption ( 'debounce' ) ) {
36
38
@@ -42,56 +44,93 @@ angular.module('angular-async-validation')
42
44
43
45
}
44
46
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 ) ) {
75
57
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
+
84
65
}
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
+
90
132
}
91
-
92
- // Add the async validator (optionally using custom name)
93
- ngModel . $asyncValidators [ validatorName ] = validationFunction ;
94
-
133
+
95
134
}
96
135
97
136
} ] ) ;
0 commit comments