This problem is starting to drive me mad and I can't seem to find any correct information or documentation regarding this problem.
I'm using symfony 4.2.3
I have a form with a dateTimeType :
->add(
'startDate',
DateTimeType::class,
[
'widget' => 'single_text',
'input' => 'datetime',
'html5' => false,
'attr' => [
'class' => 'combinedPickerInput',
'placeholder' => date('d/m/y H:i'),
],
'format' => 'd/m/Y H:i',
'date_format' => 'd/m/Y H:i',
'label' => 'form.specialOffer.startDate',
'translation_domain' => 'Default',
'required' => false,
]
)
When I submit the form using AJAX the $_POST shows ̀startDate = '24/04/2019 06:24'
However as soon as I use the getData() function on my form the value becomes a DateTime object (as it should) but it is not formatted correctly
startDate = {
date = 2018-12-24 06:04:00.000000,
timezone_type = 3
timezone = "Europe/Paris"
}
The field in my entity is
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $startDate;
I have also tried using the DateType instead of the DateTimeType but that doesn't change anything.
->add(
'endDate',
DateType::class,
[
'widget' => 'single_text',
'html5' => false,
'attr' => [
'class' => 'combinedPickerInput',
'placeholder' => date('d/m/y H:i'),
],
'format' => 'd/m/Y H:i',
'label' => 'form.specialOffer.endDate',
'translation_domain' => 'Default',
'required' => false,
]
)
The result is exactly the same as with the DateTimeType
When looking at the DateTimeType class something seems a bit off. when setting the date format the code is as follows
$dateFormat = \is_int($options['date_format']) ? $options['date_format'] : self::DEFAULT_DATE_FORMAT;
which to me makes it impossible to use a date_format that is not an int. However the validation and error message just below seem to indicate that it should in fact be possible to pass a string for a custom date format
if (!\in_array($dateFormat, self::$acceptedFormats, true)) {
throw new InvalidOptionsException('The "date_format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.');
}
I am quite lost with this any help would be greatly appreciated
Can you try to change the format option value with dd/MM/yyyy H:i and use DateTimeType instead of DateType
->add('endDate', DateType::class, [
'widget' => 'single_text',
'html5' => false,
'attr' => [
'class' => 'combinedPickerInput',
'placeholder' => date('d/m/y H:i'),
],
'format' => 'dd/MM/yyyy H:i',
'label' => 'form.specialOffer.endDate',
'translation_domain' => 'Default',
'required' => false,
])