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

Commit 4b653ae

Browse files
runkmatsko
authored andcommitted
fix(input): keep track of min/max attars on-the-fly
Now input[type=button] keeps track of both min and max attrs even if they change over time.
1 parent 269bc7e commit 4b653ae

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
544544
});
545545

546546
if (attr.min) {
547-
var min = parseFloat(attr.min);
548547
var minValidator = function(value) {
548+
var min = parseFloat(attr.min);
549549
if (!ctrl.$isEmpty(value) && value < min) {
550550
ctrl.$setValidity('min', false);
551551
return undefined;
@@ -560,8 +560,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
560560
}
561561

562562
if (attr.max) {
563-
var max = parseFloat(attr.max);
564563
var maxValidator = function(value) {
564+
var max = parseFloat(attr.max);
565565
if (!ctrl.$isEmpty(value) && value > max) {
566566
ctrl.$setValidity('max', false);
567567
return undefined;

test/ng/directive/inputSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,21 @@ describe('input', function() {
667667
expect(scope.value).toBe(100);
668668
expect(scope.form.alias.$error.min).toBeFalsy();
669669
});
670+
671+
it('should validate even if min value changes on-the-fly', function(done) {
672+
scope.min = 10;
673+
compileInput('<input type="number" ng-model="value" name="alias" min="{{min}}" />');
674+
scope.$digest();
675+
676+
changeInputValueTo('5');
677+
expect(inputElm).toBeInvalid();
678+
679+
scope.min = 0;
680+
scope.$digest(function () {
681+
expect(inputElm).toBeValid();
682+
done();
683+
});
684+
});
670685
});
671686

672687

@@ -686,6 +701,21 @@ describe('input', function() {
686701
expect(scope.value).toBe(0);
687702
expect(scope.form.alias.$error.max).toBeFalsy();
688703
});
704+
705+
it('should validate even if max value changes on-the-fly', function(done) {
706+
scope.max = 10;
707+
compileInput('<input type="number" ng-model="value" name="alias" max="{{max}}" />');
708+
scope.$digest();
709+
710+
changeInputValueTo('5');
711+
expect(inputElm).toBeValid();
712+
713+
scope.max = 0;
714+
scope.$digest(function () {
715+
expect(inputElm).toBeInvalid();
716+
done();
717+
});
718+
});
689719
});
690720

691721

0 commit comments

Comments
 (0)