Datetime and Datelist form elements are never validate when put in a table element

Created on 26 November 2018, over 6 years ago
Updated 18 August 2025, 7 days ago

I'm constructing a form presently and I have a problem when I use datetime and datelist form elements in a table. Example of code:

$form['part_a']['courses_table'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Title'),
        $this
          ->t('Hours'),
        $this
          ->t('Date (month/year)'),
        $this
          ->t('Location'),
      ]
    ];

    for ($i = 0; $i < $nb_course; $i++) {
      $form['part_a']['courses_table'] [$i] = [
        'title' => [
          '#title' => t('Title'),
          '#title_display' => 'invisible',
          '#type' => 'textfield',
          '#required' => TRUE
        ],
        'hours' => [
          '#title' => t('Hours'),
          '#title_display' => 'invisible',
          '#type' => 'number',
          '#min' => 1,
          '#required' => TRUE
        ],
        'date' => [
          //        '#title' => t('Date of Admission'),
          '#title_display' => 'invisible',
          '#date_increment' => 1,
          '#date_part_order' => ['month', 'year'],
          '#date_year_range' => ($year - 1) . ":" . $year,
          '#type' => 'datelist',
          '#required' => TRUE
        ],
        'location' => [
          '#title' => t('Location'),
          '#title_display' => 'invisible',
          '#type' => 'textfield',
          '#required' => TRUE
        ]
      ];

    }

If I use this kind on architecture in my form, the datelist element is never validate and I have always the error "The date is invalid. ". If I use a datetime, I have a similar error of type "'The date date is invalid. Please enter a date in the format 2018-26-11."

When I'm looking in the core for these both elements, it seems the bug occur because these elements aren't converted as a DrupalDateTime object for the validation

If I use the standard form element date, it works correctly but you don't have the same possibilities than with elements datelist and datetime.

Of course, outside a table, these elements work properly, it's only happen when they are use in a table. I tried to add "#tree" => TRUE to my table thinking it could be a bug due the hierarchy but it didn't fix the problem and the bug happens if the table has 1 like more elements.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

render system

Created by

🇨🇦Canada clairedesbois@gmail.com Montreal

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.

Production build 0.71.5 2024