Skip to content

Commit ccba701

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

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/AngularPublic.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
ngChangeDirective,
4646
requiredDirective,
4747
requiredDirective,
48+
minlengthDirective,
49+
maxlengthDirective,
4850
ngValueDirective,
4951
ngModelOptionsDirective,
5052
ngAttributeAliasDirectives,
@@ -184,6 +186,8 @@ function publishExternalAPI(angular){
184186
ngChange: ngChangeDirective,
185187
required: requiredDirective,
186188
ngRequired: requiredDirective,
189+
ngMinlength: minlengthDirective,
190+
ngMaxlength: maxlengthDirective,
187191
ngValue: ngValueDirective,
188192
ngModelOptions: ngModelOptionsDirective
189193
}).

src/ng/directive/input.js

Lines changed: 27 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,33 @@ 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+
var maxlength = int(attr.ngMaxlength);
2127+
ctrl.$validators.maxlength = function(value) {
2128+
return ctrl.$isEmpty(value) || value.length <= maxlength;
2129+
};
2130+
}
2131+
};
2132+
};
2133+
2134+
var minlengthDirective = function() {
2135+
return {
2136+
require: '?ngModel',
2137+
link: function(scope, elm, attr, ctrl) {
2138+
if (!ctrl) return;
2139+
var minlength = int(attr.ngMinlength);
2140+
ctrl.$validators.minlength = function(value) {
2141+
return ctrl.$isEmpty(value) || value.length >= minlength;
2142+
};
2143+
}
2144+
};
2145+
};
2146+
2147+
21372148
/**
21382149
* @ngdoc directive
21392150
* @name ngList

0 commit comments

Comments
 (0)