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

Commit 2460e42

Browse files
Jeffrey Barruswesleycho
Jeffrey Barrus
authored andcommitted
feat(datepicker): add allowInvalid support
- Adds support for invalid view values via `ngModelOptions` allowInvalid` setting Closes #4694 Closes #4837
1 parent 3658502 commit 2460e42

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Diff for: src/datepicker/datepicker.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -826,14 +826,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
826826

827827
if (angular.isString(viewValue)) {
828828
var date = parseDateString(viewValue);
829-
if (isNaN(date)) {
830-
return undefined;
829+
if (!isNaN(date)) {
830+
return date;
831831
}
832-
833-
return date;
834832
}
835833

836-
return undefined;
834+
return ngModel.$options && ngModel.$options.allowInvalid ? viewValue : undefined;
837835
}
838836

839837
function validator(modelValue, viewValue) {

Diff for: src/datepicker/docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ The datepicker has 3 modes:
9494
* `year-range`
9595
_(Default: `20`)_ -
9696
Number of years displayed in year selection.
97+
98+
* `ng-model-options`
99+
_(Default: {})_ -
100+
allowInvalid support. [More on ngModelOptions](https://docs.angularjs.org/api/ng/directive/ngModelOptions).
101+
97102

98103
### uib-datepicker-popup settings ###
99104

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

+26
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,32 @@ describe('datepicker', function() {
12661266
});
12671267
});
12681268

1269+
describe('ngModelOptions allowInvalid', function() {
1270+
var $sniffer, inputEl;
1271+
1272+
beforeEach(inject(function(_$sniffer_) {
1273+
$sniffer = _$sniffer_;
1274+
1275+
$rootScope.date = new Date('September 30, 2010 15:30:00');
1276+
$rootScope.modelOptions = {allowInvalid: true};
1277+
element = $compile('<div><input ng-model="date" ng-model-options="modelOptions" uib-datepicker-popup></div>')($rootScope);
1278+
inputEl = element.find('input');
1279+
$rootScope.$digest();
1280+
}));
1281+
1282+
function changeInputValueTo(el, value) {
1283+
el.val(value);
1284+
el.trigger($sniffer.hasEvent('input') ? 'input' : 'change');
1285+
$rootScope.$digest();
1286+
}
1287+
1288+
it('should update ng-model even if the date is invalid when allowInvalid is true', function() {
1289+
changeInputValueTo(inputEl, 'pizza');
1290+
expect($rootScope.date).toBe('pizza');
1291+
expect(inputEl.val()).toBe('pizza');
1292+
});
1293+
});
1294+
12691295
describe('setting datepickerPopupConfig', function() {
12701296
var originalConfig = {};
12711297
beforeEach(inject(function(uibDatepickerPopupConfig) {

0 commit comments

Comments
 (0)