Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit fe0d954

Browse files
nivivivewesleycho
authored andcommitted
fix(datepicker): fix validation with ngRequired
- Fix how validation is handled using `ngRequired` Closes #4002 Fixes #3862
1 parent 2e9177e commit fe0d954

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

Diff for: src/datepicker/datepicker.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
8585
} else {
8686
$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.');
8787
}
88-
ngModelCtrl.$setValidity('date', isValid);
8988
}
9089
this.refreshView();
9190
};
@@ -616,6 +615,11 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
616615

617616
function validator(modelValue, viewValue) {
618617
var value = modelValue || viewValue;
618+
619+
if (!attrs.ngRequired && !value) {
620+
return true;
621+
}
622+
619623
if (angular.isNumber(value)) {
620624
value = new Date(value);
621625
}

Diff for: src/datepicker/test/datepicker.spec.js

+35-23
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,10 @@ describe('datepicker directive', function () {
334334
expect(angular.isDate($rootScope.date)).toBe(true);
335335
});
336336

337-
it('to a date that is invalid, it gets invalid', function() {
337+
it('to a date that is invalid, it doesn\`t update', function() {
338338
$rootScope.date = new Date('pizza');
339339
$rootScope.$digest();
340-
expect(element.hasClass('ng-invalid')).toBeTruthy();
341-
expect(element.hasClass('ng-invalid-date')).toBeTruthy();
340+
expect(getTitle()).toBe('September 2010');
342341
expect(angular.isDate($rootScope.date)).toBe(true);
343342
expect(isNaN($rootScope.date)).toBe(true);
344343
});
@@ -360,11 +359,10 @@ describe('datepicker directive', function () {
360359
expect(angular.isString($rootScope.date)).toBe(true);
361360
});
362361

363-
it('to a string that cannot be parsed by Date, it gets invalid', function() {
362+
it('to a string that cannot be parsed by Date, it doesn\'t update', function() {
364363
$rootScope.date = 'pizza';
365364
$rootScope.$digest();
366-
expect(element.hasClass('ng-invalid')).toBeTruthy();
367-
expect(element.hasClass('ng-invalid-date')).toBeTruthy();
365+
expect(getTitle()).toBe('September 2010');
368366
expect($rootScope.date).toBe('pizza');
369367
});
370368
});
@@ -1876,27 +1874,41 @@ describe('datepicker directive', function () {
18761874
});
18771875

18781876
describe('use with `ng-required` directive', function() {
1879-
beforeEach(inject(function() {
1880-
$rootScope.date = '';
1881-
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="true"><div>')($rootScope);
1882-
$rootScope.$digest();
1883-
assignElements(wrapElement);
1884-
}));
1877+
describe('`ng-required is true`', function() {
1878+
beforeEach(inject(function() {
1879+
$rootScope.date = '';
1880+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="true"><div>')($rootScope);
1881+
$rootScope.$digest();
1882+
assignElements(wrapElement);
1883+
}));
18851884

1886-
it('should be invalid initially', function() {
1887-
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
1888-
});
1885+
it('should be invalid initially and when no date', function() {
1886+
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
1887+
});
18891888

1890-
it('should be valid if model has been specified', function() {
1891-
$rootScope.date = new Date();
1892-
$rootScope.$digest();
1893-
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1889+
it('should be valid if model has been specified', function() {
1890+
$rootScope.date = new Date();
1891+
$rootScope.$digest();
1892+
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1893+
});
1894+
1895+
it('should be valid if model value is a valid timestamp', function() {
1896+
$rootScope.date = Date.now();
1897+
$rootScope.$digest();
1898+
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1899+
});
18941900
});
1901+
describe('`ng-required is false`', function() {
1902+
beforeEach(inject(function() {
1903+
$rootScope.date = '';
1904+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-required="false"><div>')($rootScope);
1905+
$rootScope.$digest();
1906+
assignElements(wrapElement);
1907+
}));
18951908

1896-
it('should be valid if model value is a valid timestamp', function() {
1897-
$rootScope.date = Date.now();
1898-
$rootScope.$digest();
1899-
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1909+
it('should be valid initially and when no date', function() {
1910+
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1911+
});
19001912
});
19011913
});
19021914

0 commit comments

Comments
 (0)