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

Commit 710cd5b

Browse files
matskomhevery
authored andcommitted
feat(NgModelValidator): perform number validations on range input elements
Closes #682
1 parent 7dc55fb commit 710cd5b

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

lib/directive/ng_model_validators.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class NgModelEmailValidator implements NgValidatable {
7979
* Validates the model to see if its contents match a valid number.
8080
*/
8181
@NgDirective(selector: 'input[type=number][ng-model]')
82+
@NgDirective(selector: 'input[type=range][ng-model]')
8283
class NgModelNumberValidator implements NgValidatable {
8384
String get name => 'number';
8485

@@ -105,9 +106,13 @@ class NgModelNumberValidator implements NgValidatable {
105106
* Validates the model to see if the numeric value than or equal to the max value.
106107
*/
107108
@NgDirective(selector: 'input[type=number][ng-model][max]')
109+
@NgDirective(selector: 'input[type=range][ng-model][max]')
108110
@NgDirective(
109111
selector: 'input[type=number][ng-model][ng-max]',
110112
map: const {'ng-max': '=>max'})
113+
@NgDirective(
114+
selector: 'input[type=range][ng-model][ng-max]',
115+
map: const {'ng-max': '=>max'})
111116
class NgModelMaxNumberValidator implements NgValidatable {
112117

113118
double _max;
@@ -147,9 +152,13 @@ class NgModelMaxNumberValidator implements NgValidatable {
147152
* Validates the model to see if the numeric value is greater than or equal to the min value.
148153
*/
149154
@NgDirective(selector: 'input[type=number][ng-model][min]')
155+
@NgDirective(selector: 'input[type=range][ng-model][min]')
150156
@NgDirective(
151157
selector: 'input[type=number][ng-model][ng-min]',
152158
map: const {'ng-min': '=>min'})
159+
@NgDirective(
160+
selector: 'input[type=range][ng-model][ng-min]',
161+
map: const {'ng-min': '=>min'})
153162
class NgModelMinNumberValidator implements NgValidatable {
154163

155164
double _min;

test/directive/ng_model_validators_spec.dart

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ library ng_model_validators;
33
import '../_specs.dart';
44

55
void main() {
6+
they(should, tokens, callback, [exclusive=false]) {
7+
tokens.forEach((token) {
8+
describe(token, () {
9+
(exclusive ? iit : it)(should, () => callback(token));
10+
});
11+
});
12+
}
13+
14+
tthey(should, tokens, callback) =>
15+
they(should, tokens, callback, true);
16+
617
describe('ngModel validators', () {
718
TestBed _;
819

@@ -128,9 +139,12 @@ void main() {
128139
}));
129140
});
130141

131-
describe('[type="number"]', () {
132-
it('should validate the input field given a valid or invalid number', inject((RootScope scope) {
133-
_.compile('<input type="number" ng-model="val" probe="i" />');
142+
describe('[type="number|range"]', () {
143+
they('should validate the input field given a valid or invalid number',
144+
['range', 'number'],
145+
(type) {
146+
147+
_.compile('<input type="$type" ng-model="val" probe="i" />');
134148
Probe probe = _.rootScope.context['i'];
135149
var model = probe.directive(NgModel);
136150

@@ -162,12 +176,13 @@ void main() {
162176
model.validate();
163177
expect(model.valid).toEqual(false);
164178
expect(model.invalid).toEqual(true);
165-
}));
179+
});
166180

167-
it('should perform a max number validation if a max attribute value is present',
168-
inject((RootScope scope) {
181+
they('should perform a max number validation if a max attribute value is present',
182+
['range', 'number'],
183+
(type) {
169184

170-
_.compile('<input type="number" ng-model="val" max="10" probe="i" />');
185+
_.compile('<input type="$type" ng-model="val" max="10" probe="i" />');
171186
Probe probe = _.rootScope.context['i'];
172187
var model = probe.directive(NgModel);
173188

@@ -198,12 +213,13 @@ void main() {
198213
expect(model.invalid).toEqual(true);
199214
expect(model.hasError('max')).toBe(false);
200215
expect(model.hasError('number')).toBe(true);
201-
}));
216+
});
202217

203-
it('should perform a max number validation if a ng-max attribute value is present and/or changed',
204-
inject((RootScope scope) {
218+
they('should perform a max number validation if a ng-max attribute value is present and/or changed',
219+
['range', 'number'],
220+
(type) {
205221

206-
_.compile('<input type="number" ng-model="val" ng-max="maxVal" probe="i" />');
222+
_.compile('<input type="$type" ng-model="val" ng-max="maxVal" probe="i" />');
207223
Probe probe = _.rootScope.context['i'];
208224
var model = probe.directive(NgModel);
209225

@@ -239,12 +255,13 @@ void main() {
239255
expect(model.valid).toEqual(true);
240256
expect(model.invalid).toEqual(false);
241257
expect(model.hasError('max')).toBe(false);
242-
}));
258+
});
243259

244-
it('should perform a min number validation if a min attribute value is present',
245-
inject((RootScope scope) {
260+
they('should perform a min number validation if a min attribute value is present',
261+
['range', 'number'],
262+
(type) {
246263

247-
_.compile('<input type="number" ng-model="val" min="-10" probe="i" />');
264+
_.compile('<input type="$type" ng-model="val" min="-10" probe="i" />');
248265
Probe probe = _.rootScope.context['i'];
249266
var model = probe.directive(NgModel);
250267

@@ -275,12 +292,13 @@ void main() {
275292
expect(model.invalid).toEqual(true);
276293
expect(model.hasError('min')).toBe(false);
277294
expect(model.hasError('number')).toBe(true);
278-
}));
295+
});
279296

280-
it('should perform a min number validation if a ng-min attribute value is present and/or changed',
281-
inject((RootScope scope) {
297+
they('should perform a min number validation if a ng-min attribute value is present and/or changed',
298+
['range', 'number'],
299+
(type) {
282300

283-
_.compile('<input type="number" ng-model="val" ng-min="minVal" probe="i" />');
301+
_.compile('<input type="$type" ng-model="val" ng-min="minVal" probe="i" />');
284302
Probe probe = _.rootScope.context['i'];
285303
var model = probe.directive(NgModel);
286304

@@ -316,7 +334,7 @@ void main() {
316334
expect(model.valid).toEqual(true);
317335
expect(model.invalid).toEqual(false);
318336
expect(model.hasError('min')).toBe(false);
319-
}));
337+
});
320338
});
321339

322340
describe('pattern', () {

0 commit comments

Comments
 (0)