1
1
part of angular.directive;
2
2
3
- abstract class NgValidatable {
4
- String get name;
5
- bool isValid (value);
3
+ abstract class NgValidator {
4
+ final String name;
5
+ bool isValid (modelValue);
6
6
}
7
7
8
8
/**
@@ -13,24 +13,24 @@ abstract class NgValidatable {
13
13
@NgDirective (
14
14
selector: '[ng-model][ng-required]' ,
15
15
map: const {'ng-required' : '=>required' })
16
- class NgModelRequiredValidator implements NgValidatable {
16
+ class NgModelRequiredValidator implements NgValidator {
17
17
bool _required = true ;
18
18
19
- String get name => 'required' ;
19
+ final String name = 'required' ;
20
20
21
21
NgModelRequiredValidator (NgModel ngModel) {
22
22
ngModel.addValidator (this );
23
23
}
24
24
25
- bool isValid (value ) {
25
+ bool isValid (modelValue ) {
26
26
// Any element which isn't required is always valid.
27
27
if (! _required) return true ;
28
28
// Null is not a value, therefore not valid.
29
- if (value == null ) return false ;
29
+ if (modelValue == null ) return false ;
30
30
// Empty lists and/or strings are not valid.
31
31
// NOTE: This is an excellent use case for structural typing.
32
32
// We really want anything object that has a 'isEmpty' property.
33
- return ! ((value is List || value is String ) && value .isEmpty);
33
+ return ! ((modelValue is List || modelValue is String ) && modelValue .isEmpty);
34
34
}
35
35
36
36
set required (value) {
@@ -42,55 +42,55 @@ class NgModelRequiredValidator implements NgValidatable {
42
42
* Validates the model to see if its contents match a valid URL pattern.
43
43
*/
44
44
@NgDirective (selector: 'input[type=url][ng-model]' )
45
- class NgModelUrlValidator implements NgValidatable {
45
+ class NgModelUrlValidator implements NgValidator {
46
46
static final URL_REGEXP = new RegExp (
47
47
r'^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?' +
48
48
r'(\/|\/([\w#!:.?+=&%@!\-\/]))?$' );
49
49
50
- String get name => 'url' ;
50
+ final String name = 'url' ;
51
51
52
52
NgModelUrlValidator (NgModel ngModel) {
53
53
ngModel.addValidator (this );
54
54
}
55
55
56
- bool isValid (value ) =>
57
- value == null || value .isEmpty || URL_REGEXP .hasMatch (value );
56
+ bool isValid (modelValue ) =>
57
+ modelValue == null || modelValue .isEmpty || URL_REGEXP .hasMatch (modelValue );
58
58
}
59
59
60
60
/**
61
61
* Validates the model to see if its contents match a valid email pattern.
62
62
*/
63
63
@NgDirective (selector: 'input[type=email][ng-model]' )
64
- class NgModelEmailValidator implements NgValidatable {
64
+ class NgModelEmailValidator implements NgValidator {
65
65
static final EMAIL_REGEXP = new RegExp (
66
66
r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$' );
67
67
68
- String get name => 'email' ;
68
+ final String name = 'email' ;
69
69
70
70
NgModelEmailValidator (NgModel ngModel) {
71
71
ngModel.addValidator (this );
72
72
}
73
73
74
- bool isValid (value ) =>
75
- value == null || value .isEmpty || EMAIL_REGEXP .hasMatch (value );
74
+ bool isValid (modelValue ) =>
75
+ modelValue == null || modelValue .isEmpty || EMAIL_REGEXP .hasMatch (modelValue );
76
76
}
77
77
78
78
/**
79
79
* Validates the model to see if its contents match a valid number.
80
80
*/
81
81
@NgDirective (selector: 'input[type=number][ng-model]' )
82
82
@NgDirective (selector: 'input[type=range][ng-model]' )
83
- class NgModelNumberValidator implements NgValidatable {
84
- String get name => 'number' ;
83
+ class NgModelNumberValidator implements NgValidator {
84
+ final String name = 'number' ;
85
85
86
86
NgModelNumberValidator (NgModel ngModel) {
87
87
ngModel.addValidator (this );
88
88
}
89
89
90
- bool isValid (value ) {
91
- if (value != null ) {
90
+ bool isValid (modelValue ) {
91
+ if (modelValue != null ) {
92
92
try {
93
- num val = double .parse (value .toString ());
93
+ num val = double .parse (modelValue .toString ());
94
94
if (val.isNaN) {
95
95
return false ;
96
96
}
@@ -113,10 +113,10 @@ class NgModelNumberValidator implements NgValidatable {
113
113
@NgDirective (
114
114
selector: 'input[type=range][ng-model][ng-max]' ,
115
115
map: const {'ng-max' : '=>max' })
116
- class NgModelMaxNumberValidator implements NgValidatable {
116
+ class NgModelMaxNumberValidator implements NgValidator {
117
117
118
118
double _max;
119
- String get name => 'max' ;
119
+ final String name = 'max' ;
120
120
121
121
NgModelMaxNumberValidator (NgModel ngModel) {
122
122
ngModel.addValidator (this );
@@ -131,11 +131,11 @@ class NgModelMaxNumberValidator implements NgValidatable {
131
131
} catch (e) {};
132
132
}
133
133
134
- bool isValid (value ) {
135
- if (value == null || max == null ) return true ;
134
+ bool isValid (modelValue ) {
135
+ if (modelValue == null || max == null ) return true ;
136
136
137
137
try {
138
- num parsedValue = double .parse (value .toString ());
138
+ num parsedValue = double .parse (modelValue .toString ());
139
139
if (! parsedValue.isNaN) {
140
140
return parsedValue <= max;
141
141
}
@@ -159,10 +159,10 @@ class NgModelMaxNumberValidator implements NgValidatable {
159
159
@NgDirective (
160
160
selector: 'input[type=range][ng-model][ng-min]' ,
161
161
map: const {'ng-min' : '=>min' })
162
- class NgModelMinNumberValidator implements NgValidatable {
162
+ class NgModelMinNumberValidator implements NgValidator {
163
163
164
164
double _min;
165
- String get name => 'min' ;
165
+ final String name = 'min' ;
166
166
167
167
NgModelMinNumberValidator (NgModel ngModel) {
168
168
ngModel.addValidator (this );
@@ -177,11 +177,11 @@ class NgModelMinNumberValidator implements NgValidatable {
177
177
} catch (e) {};
178
178
}
179
179
180
- bool isValid (value ) {
181
- if (value == null || min == null ) return true ;
180
+ bool isValid (modelValue ) {
181
+ if (modelValue == null || min == null ) return true ;
182
182
183
183
try {
184
- num parsedValue = double .parse (value .toString ());
184
+ num parsedValue = double .parse (modelValue .toString ());
185
185
if (! parsedValue.isNaN) {
186
186
return parsedValue >= min;
187
187
}
@@ -202,19 +202,19 @@ class NgModelMinNumberValidator implements NgValidatable {
202
202
@NgDirective (
203
203
selector: '[ng-model][ng-pattern]' ,
204
204
map: const {'ng-pattern' : '=>pattern' })
205
- class NgModelPatternValidator implements NgValidatable {
205
+ class NgModelPatternValidator implements NgValidator {
206
206
RegExp _pattern;
207
207
208
- String get name => 'pattern' ;
208
+ final String name = 'pattern' ;
209
209
210
210
NgModelPatternValidator (NgModel ngModel) {
211
211
ngModel.addValidator (this );
212
212
}
213
213
214
- bool isValid (value ) {
214
+ bool isValid (modelValue ) {
215
215
//remember, only required validates for the input being empty
216
- return _pattern == null || value == null || value .length == 0 ||
217
- _pattern.hasMatch (value );
216
+ return _pattern == null || modelValue == null || modelValue .length == 0 ||
217
+ _pattern.hasMatch (modelValue );
218
218
}
219
219
220
220
@NgAttr ('pattern' )
@@ -231,19 +231,19 @@ class NgModelPatternValidator implements NgValidatable {
231
231
@NgDirective (
232
232
selector: '[ng-model][ng-minlength]' ,
233
233
map: const {'ng-minlength' : '=>minlength' })
234
- class NgModelMinLengthValidator implements NgValidatable {
234
+ class NgModelMinLengthValidator implements NgValidator {
235
235
int _minlength;
236
236
237
- String get name => 'minlength' ;
237
+ final String name = 'minlength' ;
238
238
239
239
NgModelMinLengthValidator (NgModel ngModel) {
240
240
ngModel.addValidator (this );
241
241
}
242
242
243
- bool isValid (value ) {
243
+ bool isValid (modelValue ) {
244
244
//remember, only required validates for the input being empty
245
- return _minlength == 0 || value == null || value .length == 0 ||
246
- value .length >= _minlength;
245
+ return _minlength == 0 || modelValue == null || modelValue .length == 0 ||
246
+ modelValue .length >= _minlength;
247
247
}
248
248
249
249
@NgAttr ('minlength' )
@@ -260,17 +260,17 @@ class NgModelMinLengthValidator implements NgValidatable {
260
260
@NgDirective (
261
261
selector: '[ng-model][ng-maxlength]' ,
262
262
map: const {'ng-maxlength' : '=>maxlength' })
263
- class NgModelMaxLengthValidator implements NgValidatable {
263
+ class NgModelMaxLengthValidator implements NgValidator {
264
264
int _maxlength = 0 ;
265
265
266
- String get name => 'maxlength' ;
266
+ final String name = 'maxlength' ;
267
267
268
268
NgModelMaxLengthValidator (NgModel ngModel) {
269
269
ngModel.addValidator (this );
270
270
}
271
271
272
- bool isValid (value ) =>
273
- _maxlength == 0 || (value == null ? 0 : value .length) <= _maxlength;
272
+ bool isValid (modelValue ) =>
273
+ _maxlength == 0 || (modelValue == null ? 0 : modelValue .length) <= _maxlength;
274
274
275
275
@NgAttr ('maxlength' )
276
276
set maxlength (value) =>
0 commit comments