Skip to content

Commit

Permalink
fix(NgModel): make ngMinlength and ngMaxlength as standalone directives
Browse files Browse the repository at this point in the history
  • Loading branch information
matsko committed May 27, 2014
1 parent bbcda9c commit 47a1d27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
ngChangeDirective,
requiredDirective,
requiredDirective,
minlengthDirective,
maxlengthDirective,
ngValueDirective,
ngModelOptionsDirective,
ngAttributeAliasDirectives,
Expand Down Expand Up @@ -184,6 +186,8 @@ function publishExternalAPI(angular){
ngChange: ngChangeDirective,
required: requiredDirective,
ngRequired: requiredDirective,
ngMinlength: minlengthDirective,
ngMaxlength: maxlengthDirective,
ngValue: ngValueDirective,
ngModelOptions: ngModelOptionsDirective
}).
Expand Down
43 changes: 27 additions & 16 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,22 +1002,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
};
}
}

// min length validator
if (attr.ngMinlength) {
var minlength = int(attr.ngMinlength);
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
};
}

// max length validator
if (attr.ngMaxlength) {
var maxlength = int(attr.ngMaxlength);
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
};
}
}

function weekParser(isoWeek) {
Expand Down Expand Up @@ -2139,6 +2123,33 @@ var requiredDirective = function() {
};


var maxlengthDirective = function() {
return {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var maxlength = int(attr.ngMaxlength);
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
};
}
};
};

var minlengthDirective = function() {
return {
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
var minlength = int(attr.ngMinlength);
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
};
}
};
};


/**
* @ngdoc directive
* @name ngList
Expand Down

0 comments on commit 47a1d27

Please sign in to comment.