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

Commit

Permalink
feat(datepicker): add allowInvalid support
Browse files Browse the repository at this point in the history
- Adds support for invalid view values via `ngModelOptions`
  allowInvalid` setting

Closes #4694
Closes #4837
  • Loading branch information
Jeffrey Barrus authored and wesleycho committed Dec 10, 2015
1 parent 3658502 commit 2460e42
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/datepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###

Expand Down
26 changes: 26 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div><input ng-model="date" ng-model-options="modelOptions" uib-datepicker-popup></div>')($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) {
Expand Down

0 comments on commit 2460e42

Please sign in to comment.