Skip to content

Commit c15d827

Browse files
committed
fix(NgModel): make ngMinlength and ngMaxlength as standalone directives
Fixes angular#6750
1 parent 999aa15 commit c15d827

File tree

3 files changed

+81
-16
lines changed

3 files changed

+81
-16
lines changed

src/AngularPublic.js

Lines changed: 8 additions & 0 deletions
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

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -998,22 +998,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
998998
return ctrl.$isEmpty(value) || isUndefined(regexp) || regexp.test(value);
999999
};
10001000
}
1001-
1002-
// min length validator
1003-
if (attr.ngMinlength) {
1004-
var minlength = int(attr.ngMinlength);
1005-
ctrl.$validators.minlength = function(value) {
1006-
return ctrl.$isEmpty(value) || value.length >= minlength;
1007-
};
1008-
}
1009-
1010-
// max length validator
1011-
if (attr.ngMaxlength) {
1012-
var maxlength = int(attr.ngMaxlength);
1013-
ctrl.$validators.maxlength = function(value) {
1014-
return ctrl.$isEmpty(value) || value.length <= maxlength;
1015-
};
1016-
}
10171001
}
10181002

10191003
function weekParser(isoWeek) {
@@ -2134,6 +2118,43 @@ var requiredDirective = function() {
21342118
};
21352119

21362120

2121+
var maxlengthDirective = function() {
2122+
return {
2123+
require: '?ngModel',
2124+
link: function(scope, elm, attr, ctrl) {
2125+
if (!ctrl) return;
2126+
2127+
var maxlength = 0;
2128+
attr.$observe('maxlength', function(value) {
2129+
maxlength = int(value) || 0;
2130+
ctrl.$validate();
2131+
});
2132+
ctrl.$validators.maxlength = function(value) {
2133+
return ctrl.$isEmpty(value) || value.length <= maxlength;
2134+
};
2135+
}
2136+
};
2137+
};
2138+
2139+
var minlengthDirective = function() {
2140+
return {
2141+
require: '?ngModel',
2142+
link: function(scope, elm, attr, ctrl) {
2143+
if (!ctrl) return;
2144+
2145+
var minlength = 0;
2146+
attr.$observe('minlength', function(value) {
2147+
minlength = int(value) || 0;
2148+
ctrl.$validate();
2149+
});
2150+
ctrl.$validators.minlength = function(value) {
2151+
return ctrl.$isEmpty(value) || value.length >= minlength;
2152+
};
2153+
}
2154+
};
2155+
};
2156+
2157+
21372158
/**
21382159
* @ngdoc directive
21392160
* @name ngList

test/ng/directive/inputSpec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,24 @@ describe('input', function() {
13231323

13241324
expect(value).toBe(5);
13251325
});
1326+
1327+
it('should observe the standard minlength attribute and register it as a validator on the model', function() {
1328+
compileInput('<input type="text" name="input" ng-model="value" minlength="{{ min }}" />');
1329+
scope.$apply(function() {
1330+
scope.min = 10;
1331+
});
1332+
1333+
changeInputValueTo('12345');
1334+
expect(inputElm).toBeInvalid();
1335+
expect(scope.form.input.$error.minlength).toBe(true);
1336+
1337+
scope.$apply(function() {
1338+
scope.min = 5;
1339+
});
1340+
1341+
expect(inputElm).toBeValid();
1342+
expect(scope.form.input.$error.minlength).not.toBe(true);
1343+
});
13261344
});
13271345

13281346

@@ -1351,6 +1369,24 @@ describe('input', function() {
13511369

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

13561392

0 commit comments

Comments
 (0)