2

is there a way to configure a date form so it has no default value? Setting the default value to NULL sets the default value automatically to the current date.

  /**
   * @return array
   */
  public function getFormFieldSampDate() {
    return [
      '#type' => 'date',
      '#title' => t('Date of Sampling '),
      '#default_value' => self::dateStringToArray($this->sampling_date),
    ];
  }

Thanks

2
  • Pretty simple, don't provide a #default_value key.
    – No Sssweat
    Mar 28, 2019 at 15:01
  • That does not work for me. The API says: "The #default_value will be today's date if no value is supplied." Thats sounds like there can't be no default date?
    – Xyarvius
    Mar 28, 2019 at 15:47

1 Answer 1

2

Step 1) Install Date module.

Step 2) Enable the Date Popup module (comes with the Date module).

Step 3) Add code like the following:

  $form['date'] = [
    '#date_label' => 'above',
    '#type' => 'date_popup',
    '#date_format' => 'm/d/Y',
    '#datepicker_options' => [
      // limits date picker so no future date can be picked.
      // remove this section if you want to allow future dates.
      'maxDate' => "+0D", 
    ],
    '#required' => TRUE,
    '#attributes' => [
      'autocomplete' => 'off',
    ],
  ];

Result:

enter image description here

3

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.