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 7, 2014
1 parent 0890edb commit f329004
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 @@ -1011,22 +1011,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 @@ -2134,6 +2118,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);

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar May 21, 2014

this means that minlength can't change once we link. Wasn't the attribute previously observed and updated every time it changed? I'm pretty sure we used to support min/max length dynamically changing. If that was the case before, we should preserve it.

This comment has been minimized.

Copy link
@Narretz

Narretz May 21, 2014

The values for minLength / maxLength are currently also not dynamically updated. The easy change is to evaluate them every time the validator is called (see e.g. bwiklund@462c5e2), but another issue is that people might expect that the validation runs immediately after the value is changed. The problems with this are discussed here: angular#5226

ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
};
}
};
};


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

0 comments on commit f329004

Please sign in to comment.