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

fix(input): re-evaluate value if min/max changed #4788

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
ctrl.$formatters.push(function(value) {
return ctrl.$isEmpty(value) ? '' : '' + value;
});

if (attr.min || attr.max) {
scope.$watch(
function() { return attr.min + '' + attr.max; },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This optimisation could fail if max and min changed at the same time in such a way that max+''+min stayed the same:

22 + '' + 23 == '2223'
2 + '' + 223 == '2223'

Admittedly, this is a highly unusual and contrived case but the performance improvement is not going to be so great (if at all) to warrant it.

function() { ctrl.$setViewValue(ctrl.$viewValue); }
);
}

if (attr.min) {
var minValidator = function(value) {
Expand Down
24 changes: 14 additions & 10 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,19 +735,21 @@ describe('input', function() {
expect(scope.form.alias.$error.min).toBeFalsy();
});

it('should validate even if min value changes on-the-fly', function(done) {
it('should validate even if min value changes on-the-fly', function() {
scope.min = 10;
compileInput('<input type="number" ng-model="value" name="alias" min="{{min}}" />');
scope.$digest();

changeInputValueTo('5');
expect(inputElm).toBeInvalid();
expect(scope.value).toBeFalsy();
expect(scope.form.alias.$error.min).toBeTruthy();

scope.min = 0;
scope.$digest(function () {
expect(inputElm).toBeValid();
done();
});
scope.$digest();
expect(inputElm).toBeValid();
expect(scope.value).toBe(5);
expect(scope.form.alias.$error.min).toBeFalsy();
});
});

Expand All @@ -769,19 +771,21 @@ describe('input', function() {
expect(scope.form.alias.$error.max).toBeFalsy();
});

it('should validate even if max value changes on-the-fly', function(done) {
it('should validate even if max value changes on-the-fly', function() {
scope.max = 10;
compileInput('<input type="number" ng-model="value" name="alias" max="{{max}}" />');
scope.$digest();

changeInputValueTo('5');
expect(inputElm).toBeValid();
expect(scope.value).toBe(5);
expect(scope.form.alias.$error.max).toBeFalsy();

scope.max = 0;
scope.$digest(function () {
expect(inputElm).toBeInvalid();
done();
});
scope.$digest();
expect(inputElm).toBeInvalid();
expect(scope.value).toBeFalsy();
expect(scope.form.alias.$error.max).toBeTruthy();
});
});

Expand Down