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

fix(datepicker): Datepicker only accept text that matches format #3152

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
closeText: 'Done',
closeOnDateSelection: true,
appendToBody: false,
showButtonBar: true
showButtonBar: true,
parseStrict: false
})

.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'dateParser', 'datepickerPopupConfig',
Expand All @@ -448,6 +449,7 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? scope.$parent.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody;

scope.showButtonBar = angular.isDefined(attrs.showButtonBar) ? scope.$parent.$eval(attrs.showButtonBar) : datepickerPopupConfig.showButtonBar;
scope.parseStrict = angular.isDefined(attrs.parseStrict) ? scope.$parent.$eval(attrs.parseStrict) : datepickerPopupConfig.parseStrict;

scope.getText = function( key ) {
return scope[key + 'Text'] || datepickerPopupConfig[key + 'Text'];
Expand Down Expand Up @@ -509,7 +511,8 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
ngModel.$setValidity('date', true);
return viewValue;
} else if (angular.isString(viewValue)) {
var date = dateParser.parse(viewValue, dateFormat) || new Date(viewValue);
var date = dateParser.parse(viewValue, dateFormat) ||
((!scope.parseStrict) ? new Date(viewValue) : undefined);
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
Expand Down
33 changes: 32 additions & 1 deletion src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ describe('datepicker directive', function () {
});

it('updates the model & calendar when input value changes', function() {
changeInputValueTo(inputEl, 'March 5, 1980');
changeInputValueTo(inputEl, '1980-03-05');

expect($rootScope.date.getFullYear()).toEqual(1980);
expect($rootScope.date.getMonth()).toEqual(2);
Expand Down Expand Up @@ -1272,6 +1272,37 @@ describe('datepicker directive', function () {
});
});

describe('attribute `parse-strict`', function () {
beforeEach(function() {
$rootScope.parseStrict = true;
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup parse-strict="parseStrict"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
});

it('true and input with invalid date creating undefined model value', function() {
changeInputValueTo(inputEl, '1');

expect($rootScope.date).toEqual(undefined);
});

});

describe('attribute `parse-strict`', function () {
beforeEach(function() {
$rootScope.parseStrict = false;
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup parse-strict="parseStrict"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
});

it('false and input with invalid date creating undefined model value', function() {
changeInputValueTo(inputEl, '1');

expect($rootScope.date).toEqual(new Date('January 01, 2001'));
});
});

describe('toggles programatically by `open` attribute', function () {
beforeEach(inject(function() {
$rootScope.open = true;
Expand Down