You are viewing limited content. For full access, please sign in.

Question

Question

How to set the date picker to a specific month for date selection

asked on August 14

I'm using Forms self-hosted, Modern Designer, and I've restricted the dates on the date picker to the month selected in a dropdown menu, so that's all well and good. However, I'm wondering if there's a way to dynamically set the date picker to open to the selected month? It currently will always open to the current month, so the user will have to navigate back in the date picker to get to the correct month to be able to set dates, which isn't the best user experience. 

So for example, if the current month is August but the user selects July from the dropdown, I'd like it to open the date picker to July, not August.

Use case for this is a reimbursement request form where they normally fill it out the month after the expenses are incurred, so I have the dropdown defaulting to the month previous to the current month, which dynamically restricts the date picker dates but still opens to the current month.

Date Picker Default Month.png
0 0

Replies

replied on August 19

If you have "restricted the dates on the date picker to the month selected in a dropdown menu", then the date picker should not show other months. I have tested that if you set the limit as 7/1 to 7/31 using field rule, then the date picker would show July on open.

If it does not work in this way, can you give more detail on how you set the limit? And which version of Forms do you use?

0 0
replied on August 19

Sure, sorry, I should have specified that I used the option to only allow specific dates using JavaScript using the following code:

const startOfMonthObj = LFForm.getFieldValues({fieldId: 44}).dateTimeObj;
const startOfMonth = dateFns.toDate(startOfMonthObj);

const endOfMonthObj = LFForm.getFieldValues({fieldId: 45}).dateTimeObj;
const endOfMonth = dateFns.toDate(endOfMonthObj);

if (dateFns.isDate(startOfMonth) === false || dateFns.isDate(endOfMonth) === false) return {
  allowed: false,
  errorMessage: 'Please select a month'
};
return date >= startOfMonth && date <= endOfMonth;

I am using Forms version 12.0.2503.10378

Also, to clarify, the dropdown is what the user sees on the UI, but the dropdown selection triggers some JS that populates two date fields with the start and end date of the selected month, which the above code uses.

0 0
replied on August 19

I can reproduce your issue now; I think it's related with setting limit with javascript.

Would you mind change to use field rules instead of setting allow specific dates using JavaScript?

Since you already have fields for start date and end date, you can simply configure field rule like this:

1 0
replied on August 20 Show version history

That does work, so thank you for that. However, if the scenario were more complex and couldn't be achieved with field rules, is there a way to achieve this without using them? Is there a way with JavaScript for instance to set the default month on the date picker? 

One scenario I could think of is if I want to only allow a user to select dynamic specific dates on a calendar based on information loaded from an external table - I don't believe this could easily be done through field rules and would require JavaScript. Would there be a way to set that to say the month of the first loaded date? I wouldn't want to restrict selection to only that month, but I would want it to start at the beginning.

Thanks!

1 0
replied on August 21

I could report this as a bug as field rule works in this way; however I think it could only set default month to the one closest to today. Otherwise if there is no starting date at all the default date would be weird.

Will that work for you?

0 0
replied on August 21

I think that would be fine in that scenario. It would be even nicer if we could pick where we want the default month to be, in case for some reason there's a range and it makes sense to have it default to the other end of the range if that's further from the current date for example, but this will still be better!

0 0
replied on August 21

Thanks for the use case, bug filed as #609662

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.