Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(input): validate minlength/maxlength for non-string values
Browse files Browse the repository at this point in the history
Use the viewValue rather than modelValue when validating. The viewValue should always be a string, and
should reflect what the user has entered, or the formatted model value.

BREAKING CHANGE:

Always uses the viewValue when validating minlength and maxlength.

Closes #7967
Closes #8811
  • Loading branch information
caitp committed Aug 29, 2014
1 parent c5f1ca3 commit 77ce5b8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2473,8 +2473,8 @@ var maxlengthDirective = function() {
maxlength = int(value) || 0;
ctrl.$validate();
});
ctrl.$validators.maxlength = function(value) {
return ctrl.$isEmpty(value) || value.length <= maxlength;
ctrl.$validators.maxlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
};
}
};
Expand All @@ -2492,8 +2492,8 @@ var minlengthDirective = function() {
minlength = int(value) || 0;
ctrl.$validate();
});
ctrl.$validators.minlength = function(value) {
return ctrl.$isEmpty(value) || value.length >= minlength;
ctrl.$validators.minlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
};
}
};
Expand Down
91 changes: 91 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,97 @@ describe('input', function() {
expect(scope.form.alias.$error.required).toBeTruthy();
});
});

describe('minlength', function() {

it('should invalidate values that are shorter than the given minlength', function() {
compileInput('<input type="number" ng-model="value" ng-minlength="3" />');

changeInputValueTo('12');
expect(inputElm).toBeInvalid();

changeInputValueTo('123');
expect(inputElm).toBeValid();
});

it('should listen on ng-minlength when minlength is observed', function() {
var value = 0;
compileInput('<input type="number" ng-model="value" ng-minlength="min" attr-capture />');
attrs.$observe('minlength', function(v) {
value = int(attrs.minlength);
});

scope.$apply(function() {
scope.min = 5;
});

expect(value).toBe(5);
});

it('should observe the standard minlength attribute and register it as a validator on the model', function() {
compileInput('<input type="number" name="input" ng-model="value" minlength="{{ min }}" />');
scope.$apply(function() {
scope.min = 10;
});

changeInputValueTo('12345');
expect(inputElm).toBeInvalid();
expect(scope.form.input.$error.minlength).toBe(true);

scope.$apply(function() {
scope.min = 5;
});

expect(inputElm).toBeValid();
expect(scope.form.input.$error.minlength).not.toBe(true);
});
});


describe('maxlength', function() {

it('should invalidate values that are longer than the given maxlength', function() {
compileInput('<input type="number" ng-model="value" ng-maxlength="5" />');

changeInputValueTo('12345678');
expect(inputElm).toBeInvalid();

changeInputValueTo('123');
expect(inputElm).toBeValid();
});

it('should listen on ng-maxlength when maxlength is observed', function() {
var value = 0;
compileInput('<input type="number" ng-model="value" ng-maxlength="max" attr-capture />');
attrs.$observe('maxlength', function(v) {
value = int(attrs.maxlength);
});

scope.$apply(function() {
scope.max = 10;
});

expect(value).toBe(10);
});

it('should observe the standard maxlength attribute and register it as a validator on the model', function() {
compileInput('<input type="number" name="input" ng-model="value" maxlength="{{ max }}" />');
scope.$apply(function() {
scope.max = 1;
});

changeInputValueTo('12345');
expect(inputElm).toBeInvalid();
expect(scope.form.input.$error.maxlength).toBe(true);

scope.$apply(function() {
scope.max = 6;
});

expect(inputElm).toBeValid();
expect(scope.form.input.$error.maxlength).not.toBe(true);
});
});
});

describe('email', function() {
Expand Down

0 comments on commit 77ce5b8

Please sign in to comment.