From 2460e423224492a35a354ee6f3dc0dd4352d00d6 Mon Sep 17 00:00:00 2001 From: Jeffrey Barrus Date: Wed, 9 Dec 2015 14:24:54 -0700 Subject: [PATCH] feat(datepicker): add allowInvalid support - Adds support for invalid view values via `ngModelOptions` allowInvalid` setting Closes #4694 Closes #4837 --- src/datepicker/datepicker.js | 8 +++----- src/datepicker/docs/readme.md | 5 +++++ src/datepicker/test/datepicker.spec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index e14a8d916a..0caa38ea63 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -826,14 +826,12 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi if (angular.isString(viewValue)) { var date = parseDateString(viewValue); - if (isNaN(date)) { - return undefined; + if (!isNaN(date)) { + return date; } - - return date; } - return undefined; + return ngModel.$options && ngModel.$options.allowInvalid ? viewValue : undefined; } function validator(modelValue, viewValue) { diff --git a/src/datepicker/docs/readme.md b/src/datepicker/docs/readme.md index 5d020916f9..bfdc1312ff 100644 --- a/src/datepicker/docs/readme.md +++ b/src/datepicker/docs/readme.md @@ -94,6 +94,11 @@ The datepicker has 3 modes: * `year-range` _(Default: `20`)_ - Number of years displayed in year selection. + +* `ng-model-options` + _(Default: {})_ - + allowInvalid support. [More on ngModelOptions](https://docs.angularjs.org/api/ng/directive/ngModelOptions). + ### uib-datepicker-popup settings ### diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index b81a47c983..57ae84b94d 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -1266,6 +1266,32 @@ describe('datepicker', function() { }); }); + describe('ngModelOptions allowInvalid', function() { + var $sniffer, inputEl; + + beforeEach(inject(function(_$sniffer_) { + $sniffer = _$sniffer_; + + $rootScope.date = new Date('September 30, 2010 15:30:00'); + $rootScope.modelOptions = {allowInvalid: true}; + element = $compile('
')($rootScope); + inputEl = element.find('input'); + $rootScope.$digest(); + })); + + function changeInputValueTo(el, value) { + el.val(value); + el.trigger($sniffer.hasEvent('input') ? 'input' : 'change'); + $rootScope.$digest(); + } + + it('should update ng-model even if the date is invalid when allowInvalid is true', function() { + changeInputValueTo(inputEl, 'pizza'); + expect($rootScope.date).toBe('pizza'); + expect(inputEl.val()).toBe('pizza'); + }); + }); + describe('setting datepickerPopupConfig', function() { var originalConfig = {}; beforeEach(inject(function(uibDatepickerPopupConfig) {