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

Commit

Permalink
fix(datepicker): parse input using dateParser
Browse files Browse the repository at this point in the history
Fixes #1289
Closes #2085
  • Loading branch information
bekos committed Apr 19, 2014
1 parent bd2ae0e commit e0eb1bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/datepicker/datepicker.js
@@ -1,4 +1,4 @@
angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position'])
angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootstrap.position'])

.constant('datepickerConfig', {
formatDay: 'dd',
Expand Down Expand Up @@ -430,8 +430,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position'])
showButtonBar: true
})

.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'datepickerPopupConfig',
function ($compile, $parse, $document, $position, dateFilter, datepickerPopupConfig) {
.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'dateParser', 'datepickerPopupConfig',
function ($compile, $parse, $document, $position, dateFilter, dateParser, datepickerPopupConfig) {
return {
restrict: 'EA',
require: 'ngModel',
Expand Down Expand Up @@ -489,7 +489,6 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
datepickerEl.attr('date-disabled', 'dateDisabled({ date: date, mode: mode })');
}

// TODO: reverse from dateFilter string to Date object
function parseDate(viewValue) {
if (!viewValue) {
ngModel.$setValidity('date', true);
Expand All @@ -498,7 +497,7 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
ngModel.$setValidity('date', true);
return viewValue;
} else if (angular.isString(viewValue)) {
var date = new Date(viewValue);
var date = dateParser.parse(viewValue, dateFormat) || new Date(viewValue);
if (isNaN(date)) {
ngModel.$setValidity('date', false);
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/datepicker/docs/demo.js
Expand Up @@ -31,6 +31,6 @@ var DatepickerDemoCtrl = function ($scope) {
};

$scope.initDate = new Date('2016-15-20');
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
};
13 changes: 13 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Expand Up @@ -1351,6 +1351,19 @@ describe('datepicker directive', function () {
});
});

describe('european format', function () {
it('dd.MM.yyyy', function() {
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup="dd.MM.yyyy"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);

changeInputValueTo(inputEl, '11.08.2013');
expect($rootScope.date.getFullYear()).toEqual(2013);
expect($rootScope.date.getMonth()).toEqual(7);
expect($rootScope.date.getDate()).toEqual(11);
});
});

describe('`close-on-date-selection` attribute', function () {
beforeEach(inject(function() {
$rootScope.close = false;
Expand Down

0 comments on commit e0eb1bc

Please sign in to comment.