Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 26d91b6

Browse files
committed
fix(NgModel): make ngMinlength and ngMaxlength as standalone directives
Fixes #6750
1 parent 5b8e7ec commit 26d91b6

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

src/AngularPublic.js

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
ngChangeDirective,
4646
requiredDirective,
4747
requiredDirective,
48+
minlengthDirective,
49+
minlengthDirective,
50+
maxlengthDirective,
51+
maxlengthDirective,
4852
ngValueDirective,
4953
ngModelOptionsDirective,
5054
ngAttributeAliasDirectives,
@@ -184,6 +188,10 @@ function publishExternalAPI(angular){
184188
ngChange: ngChangeDirective,
185189
required: requiredDirective,
186190
ngRequired: requiredDirective,
191+
ngMinlength: minlengthDirective,
192+
minlength: minlengthDirective,
193+
ngMaxlength: maxlengthDirective,
194+
maxlength: maxlengthDirective,
187195
ngValue: ngValueDirective,
188196
ngModelOptions: ngModelOptionsDirective
189197
}).

src/ng/directive/input.js

+37-16
Original file line numberDiff line numberDiff line change
@@ -1000,22 +1000,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10001000
return ctrl.$isEmpty(value) || isUndefined(regexp) || regexp.test(value);
10011001
};
10021002
}
1003-
1004-
// min length validator
1005-
if (attr.ngMinlength) {
1006-
var minlength = int(attr.ngMinlength);
1007-
ctrl.$validators.minlength = function(value) {
1008-
return ctrl.$isEmpty(value) || value.length >= minlength;
1009-
};
1010-
}
1011-
1012-
// max length validator
1013-
if (attr.ngMaxlength) {
1014-
var maxlength = int(attr.ngMaxlength);
1015-
ctrl.$validators.maxlength = function(value) {
1016-
return ctrl.$isEmpty(value) || value.length <= maxlength;
1017-
};
1018-
}
10191003
}
10201004

10211005
function weekParser(isoWeek) {
@@ -2183,6 +2167,43 @@ var requiredDirective = function() {
21832167
};
21842168

21852169

2170+
var maxlengthDirective = function() {
2171+
return {
2172+
require: '?ngModel',
2173+
link: function(scope, elm, attr, ctrl) {
2174+
if (!ctrl) return;
2175+
2176+
var maxlength = 0;
2177+
attr.$observe('maxlength', function(value) {
2178+
maxlength = int(value) || 0;
2179+
ctrl.$validate();
2180+
});
2181+
ctrl.$validators.maxlength = function(value) {
2182+
return ctrl.$isEmpty(value) || value.length <= maxlength;
2183+
};
2184+
}
2185+
};
2186+
};
2187+
2188+
var minlengthDirective = function() {
2189+
return {
2190+
require: '?ngModel',
2191+
link: function(scope, elm, attr, ctrl) {
2192+
if (!ctrl) return;
2193+
2194+
var minlength = 0;
2195+
attr.$observe('minlength', function(value) {
2196+
minlength = int(value) || 0;
2197+
ctrl.$validate();
2198+
});
2199+
ctrl.$validators.minlength = function(value) {
2200+
return ctrl.$isEmpty(value) || value.length >= minlength;
2201+
};
2202+
}
2203+
};
2204+
};
2205+
2206+
21862207
/**
21872208
* @ngdoc directive
21882209
* @name ngList

test/ng/directive/inputSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,24 @@ describe('input', function() {
13681368

13691369
expect(value).toBe(5);
13701370
});
1371+
1372+
it('should observe the standard minlength attribute and register it as a validator on the model', function() {
1373+
compileInput('<input type="text" name="input" ng-model="value" minlength="{{ min }}" />');
1374+
scope.$apply(function() {
1375+
scope.min = 10;
1376+
});
1377+
1378+
changeInputValueTo('12345');
1379+
expect(inputElm).toBeInvalid();
1380+
expect(scope.form.input.$error.minlength).toBe(true);
1381+
1382+
scope.$apply(function() {
1383+
scope.min = 5;
1384+
});
1385+
1386+
expect(inputElm).toBeValid();
1387+
expect(scope.form.input.$error.minlength).not.toBe(true);
1388+
});
13711389
});
13721390

13731391

@@ -1396,6 +1414,24 @@ describe('input', function() {
13961414

13971415
expect(value).toBe(10);
13981416
});
1417+
1418+
it('should observe the standard maxlength attribute and register it as a validator on the model', function() {
1419+
compileInput('<input type="text" name="input" ng-model="value" maxlength="{{ max }}" />');
1420+
scope.$apply(function() {
1421+
scope.max = 1;
1422+
});
1423+
1424+
changeInputValueTo('12345');
1425+
expect(inputElm).toBeInvalid();
1426+
expect(scope.form.input.$error.maxlength).toBe(true);
1427+
1428+
scope.$apply(function() {
1429+
scope.max = 6;
1430+
});
1431+
1432+
expect(inputElm).toBeValid();
1433+
expect(scope.form.input.$error.maxlength).not.toBe(true);
1434+
});
13991435
});
14001436

14011437

0 commit comments

Comments
 (0)