Date element #default_value has no effect

Created on 6 September 2019, about 5 years ago
Updated 9 November 2023, 10 months ago

When adding a Date element to a render array, the #default_value property does not work as described in the documentation. The HTML output is <input type="date" class="form-date">, with no trace of the expected default value.

Looking at the code of \Drupal\Core\Render\Element\Date::preRenderDate() I figured out that there is a workaround: the #value property. Other properties, such as #min and #max are also not documented. Using those properties you can achieve an HTML output like this: <input type="date" min="2006-07-06" max="2019-09-04" value="2019-09-04" class="form-date">

This issue is related to πŸ› Update Date element documentation for #date_date_format and usage for other date types such as time Needs work .

πŸ› Bug report
Status

Closed: cannot reproduce

Version

11.0 πŸ”₯

Component
RenderΒ  β†’

Last updated 2 days ago

Created by

πŸ‡³πŸ‡±Netherlands marcvangend Amsterdam

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Maybe this is outdated, I couldn't reproduce in 11.x. Added the following to \Drupal\system\Form\SiteInformationForm, as per the documentation in \Drupal\Core\Render\Element\Date.

    $form['expiration'] = [
      '#type' => 'date',
      '#title' => $this->t('Content expiration'),
      '#default_value' => '2020-02-15',
    ];
    

    This produced the following markup:

    <input type="date" data-drupal-selector="edit-expiration" id="edit-expiration" name="expiration" value="2020-02-15" class="form-date form-element form-element--type-date form-element--api-date"/>

Production build 0.71.5 2024