1

I have a custom module where I set two date pop-ups.

$form['set_start_date'] = array(
  '#type' => 'date',
  '#title' => t('set start date'),
  '#required' => TRUE,
);

$form['set_end_date'] = array(
  '#type' => 'date',
  '#title' => t('set end date'),
  '#required' => TRUE,
);

If I select 2017-04-12 as start date, the accepted end date should not be after 2018-04-12.

How can I achieve this?

  • 1
    No need to do custom code ,content type>managefield>edit date field ,where you can see Default date and Relative default value,set Default date=relative,Relative default value= -12 month – user52318 Mar 22 '17 at 8:54
  • @Vikramfz16 That is for a content type; the OP is creating a form, so that setting would not help. – kiamlaluno Mar 27 '17 at 6:55
0

Following Method i used to restrict the date Pop-up to 12 months

$form['set_start_date'] = [
      '#title' => $this->t('START DATE '),
      '#type' => 'date',
      '#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
      '#date_date_format' => 'd/m/Y',
    ];


 $form['set_end_date'] = [
      '#title' => $this->t('END DATE'),
      '#type' => 'date',
      '#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
      '#date_date_format' => 'd/m/Y',
    ];


  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Filter'),
    );

 return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.