diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index c0ef015837..9523a95fce 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -85,7 +85,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst } else { $log.error('Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.'); } - ngModelCtrl.$setValidity('date', isValid); } this.refreshView(); }; @@ -616,6 +615,11 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi function validator(modelValue, viewValue) { var value = modelValue || viewValue; + + if (!attrs.ngRequired && !value) { + return true; + } + if (angular.isNumber(value)) { value = new Date(value); } diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index 7bc2daff18..03909d89cd 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -334,11 +334,10 @@ describe('datepicker directive', function () { expect(angular.isDate($rootScope.date)).toBe(true); }); - it('to a date that is invalid, it gets invalid', function() { + it('to a date that is invalid, it doesn\`t update', function() { $rootScope.date = new Date('pizza'); $rootScope.$digest(); - expect(element.hasClass('ng-invalid')).toBeTruthy(); - expect(element.hasClass('ng-invalid-date')).toBeTruthy(); + expect(getTitle()).toBe('September 2010'); expect(angular.isDate($rootScope.date)).toBe(true); expect(isNaN($rootScope.date)).toBe(true); }); @@ -360,11 +359,10 @@ describe('datepicker directive', function () { expect(angular.isString($rootScope.date)).toBe(true); }); - it('to a string that cannot be parsed by Date, it gets invalid', function() { + it('to a string that cannot be parsed by Date, it doesn\'t update', function() { $rootScope.date = 'pizza'; $rootScope.$digest(); - expect(element.hasClass('ng-invalid')).toBeTruthy(); - expect(element.hasClass('ng-invalid-date')).toBeTruthy(); + expect(getTitle()).toBe('September 2010'); expect($rootScope.date).toBe('pizza'); }); }); @@ -1876,27 +1874,41 @@ describe('datepicker directive', function () { }); describe('use with `ng-required` directive', function() { - beforeEach(inject(function() { - $rootScope.date = ''; - var wrapElement = $compile('
')($rootScope); - $rootScope.$digest(); - assignElements(wrapElement); - })); + describe('`ng-required is true`', function() { + beforeEach(inject(function() { + $rootScope.date = ''; + var wrapElement = $compile('
')($rootScope); + $rootScope.$digest(); + assignElements(wrapElement); + })); - it('should be invalid initially', function() { - expect(inputEl.hasClass('ng-invalid')).toBeTruthy(); - }); + it('should be invalid initially and when no date', function() { + expect(inputEl.hasClass('ng-invalid')).toBeTruthy(); + }); - it('should be valid if model has been specified', function() { - $rootScope.date = new Date(); - $rootScope.$digest(); - expect(inputEl.hasClass('ng-valid')).toBeTruthy(); + it('should be valid if model has been specified', function() { + $rootScope.date = new Date(); + $rootScope.$digest(); + expect(inputEl.hasClass('ng-valid')).toBeTruthy(); + }); + + it('should be valid if model value is a valid timestamp', function() { + $rootScope.date = Date.now(); + $rootScope.$digest(); + expect(inputEl.hasClass('ng-valid')).toBeTruthy(); + }); }); + describe('`ng-required is false`', function() { + beforeEach(inject(function() { + $rootScope.date = ''; + var wrapElement = $compile('
')($rootScope); + $rootScope.$digest(); + assignElements(wrapElement); + })); - it('should be valid if model value is a valid timestamp', function() { - $rootScope.date = Date.now(); - $rootScope.$digest(); - expect(inputEl.hasClass('ng-valid')).toBeTruthy(); + it('should be valid initially and when no date', function() { + expect(inputEl.hasClass('ng-valid')).toBeTruthy(); + }); }); });