Skip to content

Commit 5221901

Browse files
author
Jeffrey Barrus
committed
feat(datepicker): ng-model-options: allowInvalid support, angular-ui#4694, angular-ui#4837
1 parent 3658502 commit 5221901

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

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) {

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

src/datepicker/test/datepicker.spec.js

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

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

0 commit comments

Comments
 (0)