Skip to content

Commit

Permalink
fix(input): format as string before running minlength/maxlength forma…
Browse files Browse the repository at this point in the history
…tters

Previously, minlength/maxlength would not work correctly when dealing with non-string model values.

A better fix for this has already been implemented in 1.3, but unfortunately implementing that fix
in 1.2 is a bit harder as it requires a number of commits to be backported in order to solve the
problem correctly.

Closes angular#5936
  • Loading branch information
caitp committed Sep 25, 2014
1 parent 37f2265 commit b80808a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
if (attr.ngMinlength) {
var minlength = int(attr.ngMinlength);
var minLengthValidator = function(value) {
if (typeof value !== 'string' && !ctrl.$isEmpty(value)) value = value.toString();
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
};

Expand All @@ -619,6 +620,7 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
if (attr.ngMaxlength) {
var maxlength = int(attr.ngMaxlength);
var maxLengthValidator = function(value) {
if (typeof value !== 'string' && !ctrl.$isEmpty(value)) value = value.toString();
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
};

Expand Down
28 changes: 28 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,20 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});


it('should convert non-empty non-strings to string before formatting', function() {
compileInput('<input type="text" ng-model="value" name="alias" ng-minlength="3">');
scope.value = 10;
scope.$digest();

expect(scope.form.alias.$error.minlength).toBe(true);

scope.value = 100;
scope.$digest();

expect(scope.form.alias.$error.minlength).not.toBe(true);
});
});


Expand All @@ -803,6 +817,20 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});


it('should convert non-empty non-strings to string before formatting', function() {
compileInput('<input type="text" ng-model="value" name="alias" ng-maxlength="3">');
scope.value = 1000;
scope.$digest();

expect(scope.form.alias.$error.maxlength).toBe(true);

scope.value = 100;
scope.$digest();

expect(scope.form.alias.$error.maxlength).not.toBe(true);
});
});


Expand Down

0 comments on commit b80808a

Please sign in to comment.