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

fix(input) re-validate input[type=number] element when min/max changes #4797

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
9 changes: 9 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
return ctrl.$isEmpty(value) ? '' : '' + value;
});

var refreshView = function() {
var value = element.val();
if (value == ctrl.$viewValue) {
ctrl.$setViewValue(value);
}
};

if (attr.min) {
var minValidator = function(value) {
var min = parseFloat(attr.min);
Expand All @@ -554,6 +561,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}
};

attr.$observe('min', refreshView);
ctrl.$parsers.push(minValidator);
ctrl.$formatters.push(minValidator);
}
Expand All @@ -570,6 +578,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}
};

attr.$observe('max', refreshView);
ctrl.$parsers.push(maxValidator);
ctrl.$formatters.push(maxValidator);
}
Expand Down
26 changes: 16 additions & 10 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,19 +735,22 @@ 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 +772,22 @@ 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