From 550289e1af68f7d85a5368e8d6ca20c961ceeebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Wed, 3 Sep 2014 15:42:44 -0400 Subject: [PATCH] fix(ngModel): revalidate the model when min/max expression values change for date inputs Closes #6755 --- src/ng/directive/input.js | 22 ++++- test/ng/directive/inputSpec.js | 172 ++++++++++++++++++++++++++------- 2 files changed, 155 insertions(+), 39 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 9ad168927fb1..aef4d1571229 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1082,16 +1082,30 @@ function createDateInputType(type, regexp, parseDate, format) { return ''; }); - if (attr.min) { + if (attr.min || attr.ngMin) { + var minVal; ctrl.$validators.min = function(value) { - return ctrl.$isEmpty(value) || isUndefined(attr.min) || parseDate(value) >= parseDate(attr.min); + return ctrl.$isEmpty(value) || isUndefined(minVal) || parseDate(value) >= minVal; }; + attr.$observe('min', function(val) { + minVal = parseObservedDateValue(val); + ctrl.$validate(); + }); } - if (attr.max) { + if (attr.max || attr.ngMax) { + var maxVal; ctrl.$validators.max = function(value) { - return ctrl.$isEmpty(value) || isUndefined(attr.max) || parseDate(value) <= parseDate(attr.max); + return ctrl.$isEmpty(value) || isUndefined(maxVal) || parseDate(value) <= maxVal; }; + attr.$observe('max', function(val) { + maxVal = parseObservedDateValue(val); + ctrl.$validate(); + }); + } + + function parseObservedDateValue(val) { + return isDefined(val) ? (isDate(val) ? val : parseDate(val)) : undefined; } }; } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 48f4b9bd153c..751172ac622d 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2063,9 +2063,12 @@ describe('input', function() { }); describe('min', function (){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + scope = $rootScope; + $rootScope.minVal = '2013-01'; + compileInput(''); + })); it('should invalidate', function (){ changeInputValueTo('2012-12'); @@ -2080,12 +2083,27 @@ describe('input', function() { expect(+scope.value).toBe(+new Date(2013, 6, 1)); expect(scope.form.alias.$error.min).toBeFalsy(); }); + + it('should revalidate when the min value changes', function (){ + changeInputValueTo('2013-07'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.min).toBeFalsy(); + + scope.minVal = '2014-01'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.min).toBeTruthy(); + }); }); describe('max', function(){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + scope = $rootScope; + $rootScope.maxVal = '2013-01'; + compileInput(''); + })); it('should validate', function (){ changeInputValueTo('2012-03'); @@ -2100,6 +2118,18 @@ describe('input', function() { expect(scope.value).toBeUndefined(); expect(scope.form.alias.$error.max).toBeTruthy(); }); + + it('should revalidate when the max value changes', function (){ + changeInputValueTo('2012-07'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.max).toBeFalsy(); + + scope.maxVal = '2012-01'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.max).toBeTruthy(); + }); }); }); @@ -2204,9 +2234,12 @@ describe('input', function() { }); describe('min', function (){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + scope = $rootScope; + $rootScope.minVal = '2013-W01'; + compileInput(''); + })); it('should invalidate', function (){ changeInputValueTo('2012-W12'); @@ -2221,12 +2254,26 @@ describe('input', function() { expect(+scope.value).toBe(+new Date(2013, 0, 17)); expect(scope.form.alias.$error.min).toBeFalsy(); }); + + it('should revalidate when the min value changes', function (){ + changeInputValueTo('2013-W03'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.min).toBeFalsy(); + + scope.minVal = '2014-W01'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.min).toBeTruthy(); + }); }); describe('max', function(){ - beforeEach(function (){ - compileInput(''); - }); + beforeEach(inject(function ($rootScope){ + $rootScope.maxVal = '2013-W01'; + scope = $rootScope; + compileInput(''); + })); it('should validate', function (){ changeInputValueTo('2012-W01'); @@ -2241,6 +2288,18 @@ describe('input', function() { expect(scope.value).toBeUndefined(); expect(scope.form.alias.$error.max).toBeTruthy(); }); + + it('should revalidate when the max value changes', function (){ + changeInputValueTo('2012-W03'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.max).toBeFalsy(); + + scope.maxVal = '2012-W01'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.max).toBeTruthy(); + }); }); }); @@ -2363,9 +2422,12 @@ describe('input', function() { }); describe('min', function (){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + $rootScope.minVal = '2000-01-01T12:30:00'; + scope = $rootScope; + compileInput(''); + })); it('should invalidate', function (){ changeInputValueTo('1999-12-31T01:02:00'); @@ -2380,12 +2442,27 @@ describe('input', function() { expect(+scope.value).toBe(+new Date(2000, 0, 1, 23, 2, 0)); expect(scope.form.alias.$error.min).toBeFalsy(); }); + + it('should revalidate when the min value changes', function (){ + changeInputValueTo('2000-02-01T01:02:00'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.min).toBeFalsy(); + + scope.minVal = '2010-01-01T01:02:00'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.min).toBeTruthy(); + }); }); describe('max', function (){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + $rootScope.maxVal = '2019-01-01T01:02:00'; + scope = $rootScope; + compileInput(''); + })); it('should invalidate', function (){ changeInputValueTo('2019-12-31T01:02:00'); @@ -2400,6 +2477,18 @@ describe('input', function() { expect(+scope.value).toBe(+new Date(2000, 0, 1, 1, 2, 0)); expect(scope.form.alias.$error.max).toBeFalsy(); }); + + it('should revalidate when the max value changes', function (){ + changeInputValueTo('2000-02-01T01:02:00'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.max).toBeFalsy(); + + scope.maxVal = '2000-01-01T01:02:00'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.max).toBeTruthy(); + }); }); it('should validate even if max value changes on-the-fly', function(done) { @@ -2550,9 +2639,12 @@ describe('input', function() { }); describe('min', function (){ - beforeEach(function (){ - compileInput(''); - }); + var scope; + beforeEach(inject(function ($rootScope){ + $rootScope.minVal = '09:30:00'; + scope = $rootScope; + compileInput(''); + })); it('should invalidate', function (){ changeInputValueTo('01:02:00'); @@ -2567,6 +2659,18 @@ describe('input', function() { expect(+scope.value).toBe(+new Date(1970, 0, 1, 23, 2, 0)); expect(scope.form.alias.$error.min).toBeFalsy(); }); + + it('should revalidate when the min value changes', function (){ + changeInputValueTo('23:02:00'); + expect(inputElm).toBeValid(); + expect(scope.form.alias.$error.min).toBeFalsy(); + + scope.minVal = '23:55:00'; + scope.$digest(); + + expect(inputElm).toBeInvalid(); + expect(scope.form.alias.$error.min).toBeTruthy(); + }); }); describe('max', function (){ @@ -2589,32 +2693,30 @@ describe('input', function() { }); }); - it('should validate even if max value changes on-the-fly', function(done) { - scope.max = '21:02:00'; + it('should validate even if max value changes on-the-fly', function() { + scope.max = '4:02:00'; compileInput(''); - changeInputValueTo('22:34:00'); + changeInputValueTo('05:34:00'); expect(inputElm).toBeInvalid(); - scope.max = '12:34:00'; - scope.$digest(function () { - expect(inputElm).toBeValid(); - done(); - }); + scope.max = '06:34:00'; + scope.$digest(); + + expect(inputElm).toBeValid(); }); - 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 = '08:45:00'; compileInput(''); changeInputValueTo('06:15:00'); expect(inputElm).toBeInvalid(); - scope.min = '13:50:00'; - scope.$digest(function () { - expect(inputElm).toBeValid(); - done(); - }); + scope.min = '05:50:00'; + scope.$digest(); + + expect(inputElm).toBeValid(); }); });