This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Couldn't load subscription status.
- Fork 6.7k
fix(datepicker): MinMaxInputValidation #3606
Closed
stevecavanagh
wants to merge
1
commit into
angular-ui:master
from
stevecavanagh:fix(datepicker)MinMaxInputValidation
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,7 +206,9 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst | |
| datepickerMode: '=?', | ||
| dateDisabled: '&', | ||
| customClass: '&', | ||
| shortcutPropagation: '&?' | ||
| shortcutPropagation: '&?', | ||
| minDate: '@', | ||
| maxDate: '@' | ||
| }, | ||
| require: ['datepicker', '?^ngModel'], | ||
| controller: 'DatepickerController', | ||
|
|
@@ -577,6 +579,62 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi | |
| datepickerEl.attr('custom-class', 'customClass({ date: date, mode: mode })'); | ||
| } | ||
|
|
||
| function compareDates(date1, date2) { | ||
| if (isNaN(date1) || isNaN(date2)) { | ||
| return undefined; | ||
| } | ||
| else { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move |
||
| return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()) ); | ||
| } | ||
| } | ||
|
|
||
| function getDateLimitToCheck(limitName) { | ||
| var watchDate = scope.watchData[limitName]; | ||
| if (!watchDate) { | ||
| return null; | ||
| } else { | ||
| return new Date(watchDate); | ||
| } | ||
| } | ||
|
|
||
| function isDateLimitMet(limitName, dateToCheck, viewValue) { | ||
| var parsedDate = dateParser.parse(viewValue, dateFormat); | ||
| var dateCompare = compareDates(parsedDate, dateToCheck); | ||
| if (limitName == 'minDate') { | ||
| return !dateCompare || dateCompare > 0; | ||
| } else if (limitName == 'maxDate') { | ||
| return !dateCompare || dateCompare < 0; | ||
| } | ||
| } | ||
|
|
||
| function dateLimitParseFormat(limitName, viewValue, isFormatOnly) { | ||
| var dateLimit = getDateLimitToCheck(limitName); | ||
| if (dateLimit) { | ||
| var isMet = isDateLimitMet(limitName, dateLimit, viewValue); | ||
| ngModel.$setValidity(limitName, isMet); | ||
| return (isFormatOnly || isMet) ? viewValue : undefined; | ||
| } else { | ||
| return viewValue; | ||
| } | ||
| } | ||
|
|
||
| function minLimitParse(viewValue) { | ||
| return dateLimitParseFormat('minDate', viewValue, false); | ||
| } | ||
|
|
||
| function minLimitFormat(viewValue) { | ||
| return dateLimitParseFormat('minDate', viewValue, true); | ||
| } | ||
|
|
||
| function maxLimitParse(viewValue) { | ||
| return dateLimitParseFormat('maxDate', viewValue, false); | ||
| } | ||
|
|
||
| function maxLimitFormat(viewValue) { | ||
| return dateLimitParseFormat('maxDate', viewValue, true); | ||
| } | ||
|
|
||
|
|
||
| function parseDate(viewValue) { | ||
| if (angular.isNumber(viewValue)) { | ||
| // presumably timestamp to date object | ||
|
|
@@ -616,14 +674,34 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi | |
| } | ||
| } | ||
|
|
||
| function parsing(viewValue) { | ||
| var result = parseDate(viewValue); | ||
| if (result) { | ||
| var minMet = minLimitParse(viewValue); | ||
| var maxMet = maxLimitParse(viewValue); | ||
| result = (minMet && maxMet) ? result : undefined; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| if (!isHtml5DateInput) { | ||
| // Internal API to maintain the correct ng-invalid-[key] class | ||
| ngModel.$$parserName = 'date'; | ||
| ngModel.$validators.date = validator; | ||
| ngModel.$parsers.unshift(parseDate); | ||
| ngModel.$parsers.unshift(parsing); | ||
| ngModel.$formatters.push(function (value) { | ||
| scope.date = value; | ||
| return ngModel.$isEmpty(value) ? value : dateFilter(value, dateFormat); | ||
| if (ngModel.$isEmpty(value)) { | ||
| return value; | ||
| } | ||
| else { | ||
| value = minLimitFormat(value); | ||
| if (value) { | ||
| value = maxLimitFormat(value); | ||
| } | ||
| value = maxLimitFormat(value); | ||
| return dateFilter(value, dateFormat); | ||
| } | ||
| }); | ||
| } | ||
| else { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1199,8 +1199,12 @@ describe('datepicker directive', function () { | |
| $document = _$document_; | ||
| $sniffer = _$sniffer_; | ||
| $rootScope.isopen = true; | ||
| $rootScope.format='yyyy-MM-dd'; | ||
| $rootScope.date = new Date('September 30, 2010 15:30:00'); | ||
| var wrapElement = $compile('<div><input ng-model="date" datepicker-popup is-open="isopen"><div>')($rootScope); | ||
| $rootScope.minimumDate = new Date('January 1, 1970'); | ||
| $rootScope.maximumDate = new Date('January 1, 2020'); | ||
| $rootScope.dateDisabledHandler = jasmine.createSpy('dateDisabledHandler'); | ||
| var wrapElement = $compile('<div><input ng-model="date" min-date = "minimumDate" max-date = "maximumDate" date-disabled="dateDisabledHandler(date, mode)" datepicker-popup="{{format}}" is-open="isopen"><div>')($rootScope); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change should be tested separately from the existing test suites. |
||
| $rootScope.$digest(); | ||
| assignElements(wrapElement); | ||
| })); | ||
|
|
@@ -1238,8 +1242,46 @@ describe('datepicker directive', function () { | |
| $rootScope.date = new Date('January 10, 1983 10:00:00'); | ||
| $rootScope.$digest(); | ||
| expect(inputEl.val()).toBe('1983-01-10'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| }); | ||
|
|
||
| it('updates the input correctly when model changes to a date prior to minimum date', function() { | ||
| $rootScope.date = new Date('January 10, 1963 10:00:00'); | ||
| $rootScope.$digest(); | ||
| expect(inputEl.val()).toBe('1963-01-10'); | ||
| expect(inputEl).toHaveClass('ng-invalid'); | ||
| expect(inputEl).toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| }); | ||
|
|
||
| it('input is not marked with invalid minimum date when minimum is not a valid date', function() { | ||
| expect(inputEl).not.toHaveClass('ng-invalid-min-date'); | ||
| $rootScope.minimumDate = 'pizza'; | ||
| $rootScope.$digest(); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-date'); | ||
| }); | ||
|
|
||
| it('updates the input correctly when model changes to a date past the maximum date', function() { | ||
| $rootScope.date = new Date('January 10, 2023 10:00:00'); | ||
| $rootScope.$digest(); | ||
| expect(inputEl.val()).toBe('2023-01-10'); | ||
| expect(inputEl).toHaveClass('ng-invalid'); | ||
| expect(inputEl).toHaveClass('ng-invalid-max-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-date'); | ||
| }); | ||
|
|
||
| it('input is not marked with invalid maximum date when maximum is not a valid date', function() { | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| $rootScope.maximumDate = new Date('pizza'); | ||
| $rootScope.$digest(); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| }); | ||
|
|
||
|
|
||
| it('closes the dropdown when a day is clicked', function() { | ||
| expect(dropdownEl.css('display')).not.toBe('none'); | ||
|
|
||
|
|
@@ -1290,6 +1332,40 @@ describe('datepicker directive', function () { | |
| expect(inputEl).not.toHaveClass('ng-invalid'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-date'); | ||
| }); | ||
| it('sets `ng-invalid` for date prior to minimum date', function() { | ||
| changeInputValueTo(inputEl, '1960-12-01'); | ||
|
|
||
| expect(inputEl).toHaveClass('ng-invalid'); | ||
| expect(inputEl).toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| expect(inputEl).toHaveClass('ng-invalid-date'); | ||
| expect($rootScope.date).toBeUndefined(); | ||
| expect(inputEl.val()).toBe('1960-12-01'); | ||
| }); | ||
|
|
||
| it('sets `ng-invalid` for date (european format) prior to minimum date', function() { | ||
| $rootScope.format='dd.MM.yyyy'; | ||
| $rootScope.$digest(); | ||
| changeInputValueTo(inputEl, '20.12.1960'); | ||
|
|
||
| expect(inputEl).toHaveClass('ng-invalid'); | ||
| expect(inputEl).toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-max-date'); | ||
| expect(inputEl).toHaveClass('ng-invalid-date'); | ||
| expect($rootScope.date).toBeUndefined(); | ||
| expect(inputEl.val()).toBe('20.12.1960'); | ||
| }); | ||
|
|
||
| it('sets `ng-invalid` for date past the maximum date', function() { | ||
| changeInputValueTo(inputEl, '2050-12-01'); | ||
|
|
||
| expect(inputEl).toHaveClass('ng-invalid'); | ||
| expect(inputEl).not.toHaveClass('ng-invalid-min-date'); | ||
| expect(inputEl).toHaveClass('ng-invalid-max-date'); | ||
| expect(inputEl).toHaveClass('ng-invalid-date'); | ||
| expect($rootScope.date).toBeUndefined(); | ||
| expect(inputEl.val()).toBe('2050-12-01'); | ||
| }); | ||
|
|
||
| describe('focus', function () { | ||
| beforeEach(function() { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new bindings should be optional