Skip Navigation

[Resolved] Prefill CRED date field and set a min and max range

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#649169

Tell us what you are trying to do?
I want to preset two custom date fields. A start and an end date.
The start and end date are prefilled by values from another post type (let's call dateMin and dateMax).
The start date should default to dateMin and the end date to dateMax.
Both date fields should be restricted to the date range (dateMin and dateMax).
If the start date is changed the lower bound of the end date should be changed accordingly.
Additionally I want to set a swiss date format.

I mostly solved it with a script.
One main problem is the loading time of the script seems sometimes to be late (windows.load event might be the wrong one).
The other one is that formatting the date format seems to break the default logic.

Is there any documentation that you are following?
https://toolset.com/forums/topic/pre-populate-cred-field-with-custom-user-meta/
https://toolset.com/forums/topic/stored-value-as-default-value-for-datepicker/
https://toolset.com/forums/topic/how-to-restrict-date-range-in-cred-date-pickers/
https://toolset.com/forums/topic/limit-date-range-for-cred-form-date-field/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#649974

Additionally I want to set a swiss date format...formatting the date format seems to break the default logic.
You can modify the date format in wp-admin > Settings. The following date formats are supported by the current version of CRED:
F j, Y // December 23, 2011
Y/m/d = 2011/12/23
m/d/Y = 12/23/2011
d/m/Y = 23/22/2011

One more date format will be available when Types 2.3 is released (currently in beta):
j F Y = 08 April 2018

One main problem is the loading time of the script seems sometimes to be late (windows.load event might be the wrong one).
Usually the window load event is okay - double check there are quotes around 'window'. If that does not help you can try the generic event 'cred_form_ready', triggered on the document object:

jQuery('window').bind('load', function(){
  // be sure 'window' is in quotes above
  // add your code here using jQuery instead of $
});
jQuery(document).on('cred_form_ready', function(){
  // add your code here using jQuery instead of $
});
#651823

Thanks for your reply. The setting of the date format in the wp settings helped.
Althought it had be nice to use the format 'd.m.Y'.

What remains:

jQuery('window').bind('load', function(){

does not work for me on chrome.

jQuery(window).bind('load', function(){

and

jQuery(document).bind('cred_form_ready', function(){

do work. But not all the time. Seems to be a timing issue. That's the first problem

The second problem comes if I set the range for a date field like

	var $textfield1 = jQuery('[id^=cred_form_30_1-textfield-1]');

	jQuery($textfield1).datepicker('option', {
		minDate: 1524614400000,
		maxDate: 1526428800000});

Then the default value disappears.

#652525

1. Can I log in and get a look at this form? I'd like to see what is happening in the browser. Private reply fields are enabled here.

2. Check the documentation for the datepicker UI widget here: http://api.jqueryui.com/datepicker/#option-maxDate
I don't think a timestamp is supported for min or max date. You must use a number of days, a JS date object, a string in your chosen date format, or a string with relative values like "+1m".

#653292

It seems like the window load event is behaving consistently for me on Chrome. Everything seems to be working well except when minDate and maxDate are set, the date format becomes incorrect. Then if I choose another date in the datepicker, the date format becomes correct again. Is that what you are seeing? Is this the only problem remaining?