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?

2
  • 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
    Commented Mar 22, 2017 at 8:54
  • @Vikramfz16 That is for a content type; the OP is creating a form, so that setting would not help.
    – apaderno
    Commented Mar 27, 2017 at 6:55

1 Answer 1

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 and acknowledge you have read our privacy policy.

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