Skip to content

Commit

Permalink
Datepicker: Updated the range tests so you can't navigate past the ye…
Browse files Browse the repository at this point in the history
…arRange. Fixes #7362 - Datepicker allows changing year to something outside yearRange
  • Loading branch information
fracmak authored and mikesherov committed Nov 14, 2012
1 parent 2553d61 commit eca5abd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/unit/datepicker/datepicker_options.js
Expand Up @@ -322,9 +322,10 @@ test('miscellaneous', function() {
});

test('minMax', function() {
expect( 17 );
expect( 19 );
var date,
inp = TestHelpers.datepicker.init('#inp'),
dp = $('#ui-datepicker-div'),
lastYear = new Date(2007, 6 - 1, 4),
nextYear = new Date(2009, 6 - 1, 4),
minDate = new Date(2008, 2 - 1, 29),
Expand Down Expand Up @@ -404,6 +405,11 @@ test('minMax', function() {
TestHelpers.datepicker.equalsDate(inp.datepicker('getDate'), new Date(2008, 6 - 1, 4), 'Min/max - setDate > min, < max');
inp.datepicker('option', {maxDate: null}).val('01/04/2009').datepicker('option', {minDate: minDate, maxDate: maxDate});
TestHelpers.datepicker.equalsDate(inp.datepicker('getDate'), maxDate, 'Min/max - setDate > max');

inp.datepicker('option', {yearRange: '-0:+1'}).val('01/01/' + new Date().getFullYear());
ok(dp.find(".ui-datepicker-prev").hasClass("ui-state-disabled"), "Year Range Test - previous button disabled at 1/1/minYear");
inp.datepicker("setDate", "12/30/" + new Date().getFullYear());
ok(dp.find(".ui-datepicker-next").hasClass("ui-state-disabled"), "Year Range Test - next button disabled at 12/30/maxYear");
});

test('setDate', function() {
Expand Down
14 changes: 13 additions & 1 deletion ui/jquery.ui.datepicker.js
Expand Up @@ -1742,8 +1742,20 @@ $.extend(Datepicker.prototype, {
_isInRange: function(inst, date) {
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
var minYear = null;
var maxYear = null;
var years = this._get(inst, 'yearRange');
if (years){
var yearSplit = years.split(':');
var currentYear = new Date().getFullYear();
minYear = parseInt(yearSplit[0], 10) + currentYear;
maxYear = parseInt(yearSplit[1], 10) + currentYear;
}

return ((!minDate || date.getTime() >= minDate.getTime()) &&
(!maxDate || date.getTime() <= maxDate.getTime()));
(!maxDate || date.getTime() <= maxDate.getTime()) &&
(!minYear || date.getFullYear() >= minYear) &&
(!maxYear || date.getFullYear() <= maxYear));
},

/* Provide the configuration settings for formatting/parsing. */
Expand Down

0 comments on commit eca5abd

Please sign in to comment.