From d28996897c379b7ff8431350f3da115be1d0bc83 Mon Sep 17 00:00:00 2001 From: Tobyee Date: Sat, 6 Dec 2014 21:04:55 +0800 Subject: [PATCH] fix(input): create max and/or min validation whatever the initial value is - fix issue #10307 - change tests to corresponding changes - also change tests for ngmax and ngmin (though they have no some issue) Closes #10307 --- src/ng/directive/input.js | 4 ++-- test/ng/directive/inputSpec.js | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 24bf1a8030fe..750c31f3f35e 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1230,7 +1230,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { return value; }); - if (attr.min || attr.ngMin) { + if (isDefined(attr.min) || attr.ngMin) { var minVal; ctrl.$validators.min = function(value) { return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal; @@ -1246,7 +1246,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { }); } - if (attr.max || attr.ngMax) { + if (isDefined(attr.max) || attr.ngMax) { var maxVal; ctrl.$validators.max = function(value) { return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index f034172246bd..b478e60a84a8 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -4211,12 +4211,17 @@ describe('input', function() { }); it('should validate even if min value changes on-the-fly', function() { - scope.min = 10; + scope.min = undefined; compileInput(''); + expect(inputElm).toBeValid(); changeInputValueTo('15'); expect(inputElm).toBeValid(); + scope.min = 10; + scope.$digest(); + expect(inputElm).toBeValid(); + scope.min = 20; scope.$digest(); expect(inputElm).toBeInvalid(); @@ -4252,12 +4257,17 @@ describe('input', function() { }); it('should validate even if the ngMin value changes on-the-fly', function() { - scope.min = 10; + scope.min = undefined; compileInput(''); + expect(inputElm).toBeValid(); changeInputValueTo('15'); expect(inputElm).toBeValid(); + scope.min = 10; + scope.$digest(); + expect(inputElm).toBeValid(); + scope.min = 20; scope.$digest(); expect(inputElm).toBeInvalid(); @@ -4294,12 +4304,17 @@ describe('input', function() { }); it('should validate even if max value changes on-the-fly', function() { - scope.max = 10; + scope.max = undefined; compileInput(''); + expect(inputElm).toBeValid(); changeInputValueTo('5'); expect(inputElm).toBeValid(); + scope.max = 10; + scope.$digest(); + expect(inputElm).toBeValid(); + scope.max = 0; scope.$digest(); expect(inputElm).toBeInvalid(); @@ -4335,12 +4350,17 @@ describe('input', function() { }); it('should validate even if the ngMax value changes on-the-fly', function() { - scope.max = 10; + scope.max = undefined; compileInput(''); + expect(inputElm).toBeValid(); changeInputValueTo('5'); expect(inputElm).toBeValid(); + scope.max = 10; + scope.$digest(); + expect(inputElm).toBeValid(); + scope.max = 0; scope.$digest(); expect(inputElm).toBeInvalid();