Skip to content

Commit 47a1d27

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

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
@@ -1002,22 +1002,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10021002
};
10031003
}
10041004
}
1005-
1006-
// min length validator
1007-
if (attr.ngMinlength) {
1008-
var minlength = int(attr.ngMinlength);
1009-
ctrl.$validators.minlength = function(value) {
1010-
return ctrl.$isEmpty(value) || value.length >= minlength;
1011-
};
1012-
}
1013-
1014-
// max length validator
1015-
if (attr.ngMaxlength) {
1016-
var maxlength = int(attr.ngMaxlength);
1017-
ctrl.$validators.maxlength = function(value) {
1018-
return ctrl.$isEmpty(value) || value.length <= maxlength;
1019-
};
1020-
}
10211005
}
10221006

10231007
function weekParser(isoWeek) {
@@ -2139,6 +2123,33 @@ var requiredDirective = function() {
21392123
};
21402124

21412125

2126+
var maxlengthDirective = function() {
2127+
return {
2128+
require: '?ngModel',
2129+
link: function(scope, elm, attr, ctrl) {
2130+
if (!ctrl) return;
2131+
var maxlength = int(attr.ngMaxlength);
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+
var minlength = int(attr.ngMinlength);
2145+
ctrl.$validators.minlength = function(value) {
2146+
return ctrl.$isEmpty(value) || value.length >= minlength;
2147+
};
2148+
}
2149+
};
2150+
};
2151+
2152+
21422153
/**
21432154
* @ngdoc directive
21442155
* @name ngList

0 commit comments

Comments
 (0)