vimuth
vimuth

Reputation: 5612

Change symfony date type input display format

Hi I am using Symfony Date-Type with widget single_text. This is my code.

use Symfony\Component\Form\Extension\Core\Type\DateType;
// ...

$builder->add('date_created', DateType::class, [
    'widget' => 'single_text',
    // this is actually the default format for single_text
    'format' => 'yyyy-MM-dd',
]);

And I'm getting this output.

enter image description here

But I need to display it as mm/dd/yyyy. Is there a way to do that?

Upvotes: 0

Views: 3773

Answers (1)

VladRia
VladRia

Reputation: 1587

There are two options:

  • use html5 (your current setting). In this case documentation says "If you want your field to be rendered as an HTML5 "date" field, you have to use a single_text widget with the yyyy-MM-dd format...". Actually browser shows you html5 widget with 30/12/2019 value but sends 2019-12-30 on submit. Check yourself: https://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_date

  • not use html5:

$builder->add('date_created', DateType::class, [
    'widget' => 'single_text',
    'html5' => false,
    'format' => 'yyyy-MM-dd',
]);

You will be able to set and submit date in yyyy-MM-dd format.

Upvotes: 3

Related Questions