Score:1

How can we limit the end date selection to +90 days from now on core Date Range widget?

ng flag

We're using Drupal 9's core Date Range module and need to limit the end date selection to no more than 90 days from today.

I've searched far and wide and have not come up with a workable solution. Seems like it should be simple but I'm stumped.

enter image description here

ru flag
This should be closed because there is no focussed question here. But as a general hint, you should create a custom constraint: https://www.drupal.org/docs/drupal-apis/entity-api/entity-validation-api/defining-constraints-validations-on-entities-andor-fields
cn flag
I read the question as, "How to limit the date selection **in the UI widget** to +90 days?" In such a case, I don't think a constraint helps because the constraint is applied when the form is submitted, so the widget will still allow selecting an invalid date (which the constraint will block after submission).
Score:0
ng flag

Solved it with hook_form_alter():

 $limit = date("Y-m-d", strtotime("+" . 90 . " day"));
 $form['field_job_posting_date']['widget'][0]['end_value']['#attributes']['max'] = $limit;
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.