Search code examples
phpsymfonydatedatepickerformbuilder

How can I change the format of my formbuilder DateType field (Symfony 4)?


This is how I create my datepicker field:

  $formBuilder->add('getTimestamp', DateType::class, array(
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd',
            'data' => new \DateTime(),
            'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
          ));

enter image description here

When I change the format...

$formBuilder->add('getTimestamp', DateType::class, array(
            'widget' => 'single_text',
            'format' => 'dd.MM.yyyy',
            'attr' => array('class' => 'form-control', 'style' => 'line-height: 20px;'), 'label' => $field['fieldName'],
          ));

... the date is formatted correctly, but the datepicker ist not working anymore.

enter image description here


Solution

  • Take a look at this: Is there any way to change input type="date" format?

    Mainly, there is a difference between "Over the wire" and "Presentation" format. In your case, the format you are configuring is "Over the wire". You should probably look at specifying a "Presentation" format in your front-end (e.g. the HTML date input, or jquery UI plugin, etc.