Search code examples
phpsymfonydatetimeparsingflatpickr

Symfony 4 - Flatpickr parse date


in my Symfony 4 project, I'm using the flatpickr plugin for datetime. https://flatpickr.js.org/

So in my formType, my date field is in Text format:

->add('date', TextType::class, [
    'label' => "Date de la mission",
    'attr'=> [
        'placeholder' => "Date de la mission"
    ],
])

But in my entity, it's a datetime.

So, how can I parse it before submitting to give a datetime ? Because in the view, it's a string. I seen this : https://flatpickr.js.org/instance-methods-properties-elements/#useful-static-methods

But I don't understand how can I apply it...

For the moment, I've :

<script>
    $("#ordre_mission_date").flatpickr({
        enableTime: true,
        dateFormat: "d/m/Y H:i",
        locale: "fr",
        time_24hr: true,
    });
</script>

Can someone help me please ?


Solution

  • You need to change your date field definition. Instead of TextType, use the DateTimeType:

    ->add('date', DateTimeType::class, [
        'label' => "Date de la mission",
        'widget' => 'single_text',
        'attr'=> [
            'placeholder' => "Date de la mission"
        ],
    ])
    

    The line 'widget' => 'single_text' will render the DateTime field as a single <input type="text"/>, allowing you to use your plugin.