Skip to content

Commit a710c87

Browse files
committed
fix(datepickerPopup): support ngModelOptions.allowInvalid angular-ui#6054
1 parent a2dee1b commit a710c87

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ lib-cov
1010
*.swo
1111
.DS_Store
1212
.idea
13+
.vscode
1314

1415
pids
1516
logs

src/datepickerPopup/popup.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,22 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
125125

126126
$scope.date = dateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));
127127

128+
if (ngModelOptions.getOption('allowInvalid') && isNaN($scope.date)) {
129+
$scope.date = value;
130+
return value;
131+
}
132+
128133
return dateParser.filter($scope.date, dateFormat);
129134
});
130135
} else {
131136
ngModel.$formatters.push(function(value) {
132137
$scope.date = dateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));
138+
139+
if (ngModelOptions.getOption('allowInvalid') && isNaN($scope.date)) {
140+
$scope.date = value;
141+
return value;
142+
}
143+
133144
return value;
134145
});
135146
}
@@ -191,7 +202,7 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
191202
} else if (angular.isDate($scope.datepickerOptions[key])) {
192203
dates[key] = new Date($scope.datepickerOptions[key]);
193204
} else {
194-
if ($datepickerPopupLiteralWarning) {
205+
if ($datepickerPopupLiteralWarning && !ngModelOptions.getOption('allowInvalid')) {
195206
$log.warn('Literal date support has been deprecated, please switch to date object usage');
196207
}
197208

src/datepickerPopup/test/popup.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,31 @@ describe('datepicker popup', function() {
580580
});
581581
});
582582

583+
describe('works with ngModelOptions allowInvalid', function() {
584+
var $timeout, wrapElement;
585+
586+
beforeEach(inject(function(_$document_, _$sniffer_, _$timeout_) {
587+
$document = _$document_;
588+
$timeout = _$timeout_;
589+
$rootScope.date = 'Invalid Date';
590+
wrapElement = $compile('<div><input ng-model="date" ' +
591+
'ng-model-options="{allowInvalid: true}" ' +
592+
'uib-datepicker-popup><div>')($rootScope);
593+
$rootScope.$digest();
594+
assignElements(wrapElement);
595+
}));
596+
597+
it('should initially display invalid date', function() {
598+
expect(inputEl.val()).toEqual('Invalid Date');
599+
});
600+
601+
it('should display invalid date on change', function(){
602+
$rootScope.date = 'Another Invalid Date';
603+
$rootScope.$digest();
604+
expect(inputEl.val()).toEqual('Another Invalid Date');
605+
});
606+
});
607+
583608
describe('attribute `datepickerOptions`', function() {
584609
describe('show-weeks', function() {
585610
beforeEach(function() {

0 commit comments

Comments
 (0)