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

Commit 40265bf

Browse files
matskomhevery
authored andcommitted
refactor(NgValidator): change interface and variable names
1 parent f572ba0 commit 40265bf

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

lib/directive/ng_model.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class NgModel extends NgControl implements NgAttachAware {
2121

2222
var _lastValue;
2323
String _exp;
24-
final _validators = <NgValidatable>[];
24+
final _validators = <NgValidator>[];
2525

2626
Watch _removeWatch;
2727
bool _watchCollection;
@@ -125,15 +125,15 @@ class NgModel extends NgControl implements NgAttachAware {
125125
/**
126126
* Registers a validator into the model to consider when running validate().
127127
*/
128-
void addValidator(NgValidatable v) {
128+
void addValidator(NgValidator v) {
129129
validators.add(v);
130130
validate();
131131
}
132132

133133
/**
134134
* De-registers a validator from the model.
135135
*/
136-
void removeValidator(NgValidatable v) {
136+
void removeValidator(NgValidator v) {
137137
validators.remove(v);
138138
validate();
139139
}

lib/directive/ng_model_validators.dart

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of angular.directive;
22

3-
abstract class NgValidatable {
4-
String get name;
5-
bool isValid(value);
3+
abstract class NgValidator {
4+
final String name;
5+
bool isValid(modelValue);
66
}
77

88
/**
@@ -13,24 +13,24 @@ abstract class NgValidatable {
1313
@NgDirective(
1414
selector: '[ng-model][ng-required]',
1515
map: const {'ng-required': '=>required'})
16-
class NgModelRequiredValidator implements NgValidatable {
16+
class NgModelRequiredValidator implements NgValidator {
1717
bool _required = true;
1818

19-
String get name => 'required';
19+
final String name = 'required';
2020

2121
NgModelRequiredValidator(NgModel ngModel) {
2222
ngModel.addValidator(this);
2323
}
2424

25-
bool isValid(value) {
25+
bool isValid(modelValue) {
2626
// Any element which isn't required is always valid.
2727
if (!_required) return true;
2828
// Null is not a value, therefore not valid.
29-
if (value == null) return false;
29+
if (modelValue == null) return false;
3030
// Empty lists and/or strings are not valid.
3131
// NOTE: This is an excellent use case for structural typing.
3232
// 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);
3434
}
3535

3636
set required(value) {
@@ -42,55 +42,55 @@ class NgModelRequiredValidator implements NgValidatable {
4242
* Validates the model to see if its contents match a valid URL pattern.
4343
*/
4444
@NgDirective(selector: 'input[type=url][ng-model]')
45-
class NgModelUrlValidator implements NgValidatable {
45+
class NgModelUrlValidator implements NgValidator {
4646
static final URL_REGEXP = new RegExp(
4747
r'^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?' +
4848
r'(\/|\/([\w#!:.?+=&%@!\-\/]))?$');
4949

50-
String get name => 'url';
50+
final String name = 'url';
5151

5252
NgModelUrlValidator(NgModel ngModel) {
5353
ngModel.addValidator(this);
5454
}
5555

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);
5858
}
5959

6060
/**
6161
* Validates the model to see if its contents match a valid email pattern.
6262
*/
6363
@NgDirective(selector: 'input[type=email][ng-model]')
64-
class NgModelEmailValidator implements NgValidatable {
64+
class NgModelEmailValidator implements NgValidator {
6565
static final EMAIL_REGEXP = new RegExp(
6666
r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$');
6767

68-
String get name => 'email';
68+
final String name = 'email';
6969

7070
NgModelEmailValidator(NgModel ngModel) {
7171
ngModel.addValidator(this);
7272
}
7373

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);
7676
}
7777

7878
/**
7979
* Validates the model to see if its contents match a valid number.
8080
*/
8181
@NgDirective(selector: 'input[type=number][ng-model]')
8282
@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';
8585

8686
NgModelNumberValidator(NgModel ngModel) {
8787
ngModel.addValidator(this);
8888
}
8989

90-
bool isValid(value) {
91-
if (value != null) {
90+
bool isValid(modelValue) {
91+
if (modelValue != null) {
9292
try {
93-
num val = double.parse(value.toString());
93+
num val = double.parse(modelValue.toString());
9494
if (val.isNaN) {
9595
return false;
9696
}
@@ -113,10 +113,10 @@ class NgModelNumberValidator implements NgValidatable {
113113
@NgDirective(
114114
selector: 'input[type=range][ng-model][ng-max]',
115115
map: const {'ng-max': '=>max'})
116-
class NgModelMaxNumberValidator implements NgValidatable {
116+
class NgModelMaxNumberValidator implements NgValidator {
117117

118118
double _max;
119-
String get name => 'max';
119+
final String name = 'max';
120120

121121
NgModelMaxNumberValidator(NgModel ngModel) {
122122
ngModel.addValidator(this);
@@ -131,11 +131,11 @@ class NgModelMaxNumberValidator implements NgValidatable {
131131
} catch(e) {};
132132
}
133133

134-
bool isValid(value) {
135-
if (value == null || max == null) return true;
134+
bool isValid(modelValue) {
135+
if (modelValue == null || max == null) return true;
136136

137137
try {
138-
num parsedValue = double.parse(value.toString());
138+
num parsedValue = double.parse(modelValue.toString());
139139
if (!parsedValue.isNaN) {
140140
return parsedValue <= max;
141141
}
@@ -159,10 +159,10 @@ class NgModelMaxNumberValidator implements NgValidatable {
159159
@NgDirective(
160160
selector: 'input[type=range][ng-model][ng-min]',
161161
map: const {'ng-min': '=>min'})
162-
class NgModelMinNumberValidator implements NgValidatable {
162+
class NgModelMinNumberValidator implements NgValidator {
163163

164164
double _min;
165-
String get name => 'min';
165+
final String name = 'min';
166166

167167
NgModelMinNumberValidator(NgModel ngModel) {
168168
ngModel.addValidator(this);
@@ -177,11 +177,11 @@ class NgModelMinNumberValidator implements NgValidatable {
177177
} catch(e) {};
178178
}
179179

180-
bool isValid(value) {
181-
if (value == null || min == null) return true;
180+
bool isValid(modelValue) {
181+
if (modelValue == null || min == null) return true;
182182

183183
try {
184-
num parsedValue = double.parse(value.toString());
184+
num parsedValue = double.parse(modelValue.toString());
185185
if (!parsedValue.isNaN) {
186186
return parsedValue >= min;
187187
}
@@ -202,19 +202,19 @@ class NgModelMinNumberValidator implements NgValidatable {
202202
@NgDirective(
203203
selector: '[ng-model][ng-pattern]',
204204
map: const {'ng-pattern': '=>pattern'})
205-
class NgModelPatternValidator implements NgValidatable {
205+
class NgModelPatternValidator implements NgValidator {
206206
RegExp _pattern;
207207

208-
String get name => 'pattern';
208+
final String name = 'pattern';
209209

210210
NgModelPatternValidator(NgModel ngModel) {
211211
ngModel.addValidator(this);
212212
}
213213

214-
bool isValid(value) {
214+
bool isValid(modelValue) {
215215
//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);
218218
}
219219

220220
@NgAttr('pattern')
@@ -231,19 +231,19 @@ class NgModelPatternValidator implements NgValidatable {
231231
@NgDirective(
232232
selector: '[ng-model][ng-minlength]',
233233
map: const {'ng-minlength': '=>minlength'})
234-
class NgModelMinLengthValidator implements NgValidatable {
234+
class NgModelMinLengthValidator implements NgValidator {
235235
int _minlength;
236236

237-
String get name => 'minlength';
237+
final String name = 'minlength';
238238

239239
NgModelMinLengthValidator(NgModel ngModel) {
240240
ngModel.addValidator(this);
241241
}
242242

243-
bool isValid(value) {
243+
bool isValid(modelValue) {
244244
//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;
247247
}
248248

249249
@NgAttr('minlength')
@@ -260,17 +260,17 @@ class NgModelMinLengthValidator implements NgValidatable {
260260
@NgDirective(
261261
selector: '[ng-model][ng-maxlength]',
262262
map: const {'ng-maxlength': '=>maxlength'})
263-
class NgModelMaxLengthValidator implements NgValidatable {
263+
class NgModelMaxLengthValidator implements NgValidator {
264264
int _maxlength = 0;
265265

266-
String get name => 'maxlength';
266+
final String name = 'maxlength';
267267

268268
NgModelMaxLengthValidator(NgModel ngModel) {
269269
ngModel.addValidator(this);
270270
}
271271

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;
274274

275275
@NgAttr('maxlength')
276276
set maxlength(value) =>

0 commit comments

Comments
 (0)