'Symfony DateTime format errors - datatransformation (external datepicker)

I'm working with Symfony 6 and a TempusDomini datepicker. I have read (amongst many others) the symfony documentation, https://symfony.com/doc/current/reference/forms/types/datetime.html. Based on the documentation I found I initially implemented the form builder as followed:

->add('TextDateField')
->add('RegistrationDateTime', DateTimeType::class, [
                'required' => false,
                'widget' => 'single_text',
                'format' => 'm/d/Y, h:i a',
                'attr' => [
                    'class' => 'datetimepicker-input',
                    'data-target' => '#datetimepicker1'
                ],
                'html5' => false,
            ])
        

The "TextDateField" is for troubleshooting, see explanation below. And the actual TWIG file looks like this (for the TextDateField I have a similar datapicker block as the one for RegistrationDateTime):

{{form_start(form)}}
{{form_row(form.username)}}

                <div class> 
                    <label for="datetimepicker1Input" class="form-label">Simple picker</label>
                    <div class="input-group" id="datetimepicker1" data-td-target-input="nearest" data-td-target-toggle="nearest">
                        <input id="user_creation_form_RegistrationDateTime" name="user_creation_form[RegistrationDateTime]" type="text" class="form-control" data-td-target="#datetimepicker1">
                        <span class="input-group-text" data-td-target="#datetimepicker1" data-td-toggle="datetimepicker">
               <span class="fa-solid fa-calendar"></span> 
             </span>
 
                </div>
                <script type="text/javascript">
                    const picker = new tempusDominus.TempusDominus(document.getElementById('datetimepicker1'));
                </script>
             </div>
 
{{form_row(form.roles)}}
{{form_row(form._token)}}
{{form_row(form.submit)}}

What I notice is that the dates are correctly given from a datepicker to the controller if passed via Text. Please see this dump where you can see 2 datepicker values as passed to the controller, both for the same dates (6 may 2022)

App\Entity\TUser^ {#361
  -id: null
  -username: "scdsqd"
  -roles: array:2 [
    0 => "ROLE_USER"
    1 => "ROLE_ADMIN"
  ]
  -TextDateField: "05/06/2022, 12:42 PM"
  -RegistrationDateTime: DateTime @1640599500 {#828
    date: 2021-12-27 11:05:00.0 Europe/Berlin (+01:00)
  }
}

So as you can see the text is passed correctly for the "text" datepicker but using the symfony DateTimeType::class messes it up by giving a seemingly random date. Using other formats as datetime (for example timestamp) is not possible.

So how can I pass the value correctly to the database?

One possible "solution" is to work with TextType::class but since I want to keep the field RegistrationDateTime in the database as a DateTimeType this is not the best solution either since that would mean I have to transform the data.

Maybe I'm not looking at the right place, but it seems to me as if Symfony is not well adjusted to use external datepickers. I have found tons of post with people struggling to implement a datepicker...

Is the approach I use correct?

How can I "force" symfony to "understand" the datepicker provided dates?

If the only way is to use TextType in the form-builder, how do I tackle the datatransformations to make sure that via doctrine I save in the datetime format?

Any pointers in the right direction are welcome... I'm getting rather frustrated by it since I've struggled on it an embarrassingly long time now...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source