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

fix(input): keep track of min/max attrs on-the-fly #4553

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
4 changes: 2 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
});

if (attr.min) {
var min = parseFloat(attr.min);
var minValidator = function(value) {
var min = parseFloat(attr.min);
if (!ctrl.$isEmpty(value) && value < min) {
ctrl.$setValidity('min', false);
return undefined;
Expand All @@ -552,8 +552,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}

if (attr.max) {
var max = parseFloat(attr.max);
var maxValidator = function(value) {
var max = parseFloat(attr.max);
if (!ctrl.$isEmpty(value) && value > max) {
ctrl.$setValidity('max', false);
return undefined;
Expand Down
30 changes: 30 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,21 @@ describe('input', function() {
expect(scope.value).toBe(100);
expect(scope.form.alias.$error.min).toBeFalsy();
});

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

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

scope.min = 0;
scope.$digest(function () {
expect(inputElm).toBeValid();
done();
});
});
});


Expand All @@ -686,6 +701,21 @@ describe('input', function() {
expect(scope.value).toBe(0);
expect(scope.form.alias.$error.max).toBeFalsy();
});

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

changeInputValueTo('5');
expect(inputElm).toBeValid();

scope.max = 0;
scope.$digest(function () {
expect(inputElm).toBeInvalid();
done();
});
});
});


Expand Down