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 the 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 happen if the table has 1 like more elements.