Opened 10 years ago

Closed 5 years ago

#5446 closed feature (wontfix)

public function for querying a date's 'selectable' status

Reported by: andrewroazen Owned by:
Priority: major Milestone: none
Component: ui.datepicker Version: 1.8
Keywords: selectable validate Cc:
Blocked by: Blocking:

Description

The datepicker does not validate against manually entered dates that are blocked out through minDate/maxDate/beforeShowDay. Given the fact that applications are likely to generate datepickers programmatically, it is inconvenient for the developer to independently replicate the data structure and logic already inside ui.datepicker to retrieve a date's status for purposes such as validation.

With one public function, the datepicker object can return a boolean 'selectable' value for any passed date (or default to the current datepicker getDate if none is passed):

	/* Get the selectable status for a date in a jQuery selection.
	   @param  target  element - the target input field or division or span
	   @param  date    Date - the date being queried (defaults to the element's current date)
	   @return selectable - the status */
	_isSelectableDatepicker: function(target,date) {
		date = date || this._getDateDatepicker(target);
		var inst = this._getInst(target);
		if (inst && !inst.inline)
			var minDate = this._getMinMaxDate(inst, 'min', true);
			var maxDate = this._getMinMaxDate(inst, 'max');
			var beforeShowDay = this._get(inst, 'beforeShowDay');
			var daySettings = (beforeShowDay ? beforeShowDay.apply((inst.input ? inst.input[0] : null), [date]) : [true, '']);
			var selectable = !(!daySettings[0] || (minDate !=null && (date < minDate)) || (maxDate !=null && (date > maxDate)));
		return (inst ? selectable : null);
	},

and the argument parser is amended to include this function's name:

if (typeof options == 'string' && (options == 'isDisabled' || options == 'getDate' || options == 'widget' || options == 'isSelectable'))

A sample usage would be as follows:

	jQuery.validator.addMethod("dpicker", function(value, element) {
		selectable = $(element).datepicker("isSelectable");
		return (this.optional(element) || selectable);
	}, "Please choose a valid date");

To query a specific date in a datepicker, the syntax would be:

		selectable = $(element).datepicker("isSelectable",date);

where date is a date object rather than a parseable string (given the utility functions already baked into datepicker, this seems fair).

Attachments (1)

jquery.ui.datepickerIS(corrected).js.zip (18.5 KB) - added by andrewroazen 10 years ago.
patched version of jquery.ui.datepicker.js

Download all attachments as: .zip

Change History (7)

Changed 10 years ago by andrewroazen

patched version of jquery.ui.datepicker.js

comment:1 Changed 10 years ago by andrewroazen

Also, for the 1.9 version the function would look as follows (if I understand the refactoring correctly):

/* Get the selectable status for a date in a jQuery selection.
   @param  date    Date - the date being queried (defaults to the element's current date)
   @return selectable - the status */
isSelectable: function(date) {
	date = date || this.retrieveDate();
	var minDate = this._get('minDate');
	var maxDate = this._get('maxDate');
	var onDate = this._get('onDate');
	onDate = ($.isFunction(onDate) ? onDate : null);
	var dateInfo = (!onDate ? {} : onDate.apply(this.element[0], [date]));
	var selectable = (dateInfo.selectable == null || dateInfo.selectable) &&
		(!minDate || date.getTime() >= minDate.getTime()) &&
		(!maxDate || date.getTime() <= maxDate.getTime());
	return selectable;
}

comment:2 Changed 8 years ago by Jörn Zaefferer

Status: newopen

We should make sure that the rewrite takes this into account.

comment:3 Changed 7 years ago by Scott González

Type: enhancementfeature

comment:4 Changed 7 years ago by Scott González

#7279 is a duplicate of this ticket.

comment:5 Changed 5 years ago by Scott González

Milestone: TBDnone

comment:6 Changed 5 years ago by Scott González

Resolution: wontfix
Status: openclosed

This is actually not that inconvenient. You're just checking if the date is less than the min, greater than the max, or within your own custom list of dates. Given the lack of requests for this and the relatively simple implementation outside of the datepicker, and the fact that the datepicker would need to invoke a callback related to rendering when not actually rendering, this isn't something we're inclined to implement.

Note: See TracTickets for help on using tickets.