Angelo Bruno
Angelo Bruno

Reputation: 11

Symfony: How to handle date field in a form

I'm using Symfony 2.6...

I have some questions about date fields in a form!

1) If i use {{ form_row (form.data) }} on a date field in twig file the system renders it as a date in US/EN style (month day year)... can i obtain it in EU/IT style?

3) How form_row works with date?

4) Is it possible to have the month in italian? and possibly in long form? (my clients are old italian people)

Upvotes: 0

Views: 574

Answers (2)

Frank B
Frank B

Reputation: 3697

Be sure that php's intl extension is enabled on your webserver and that you changed en to it in app/config/config.yml at the default_locale: option and the order of year/month/day should change to the italian standard.

An other solution is to change te format for that single field. You can read it in the docs.

Upvotes: 1

Motoroller
Motoroller

Reputation: 479

1) Probably you get that format because it is defined in a default form style. The easiest way is to override that style in the twig you are going to use using form customisation(http://symfony.com/doc/current/cookbook/form/form_customization.html).

If you define {% form_theme form_self %} in the beginning of the twig block, you can override your default form type which is defined in form_div_layout.html.twig(find the link to the code inside the page cited above). Inside that code, the rendering part of the date will be around {%- block date_widget -%}, so if you can redefine this block in your twig, probably that will change the default behavior of

{{form_row (form.data)}}

3) form_row returns a set of field's label, errors and widget(http://symfony.com/doc/current/reference/forms/twig_reference.html). In the case of date, it will usually return label and date string in a format defined in your form style.

4)How would you exactly like to express the month? If it can be separate and only have to be rendered in a confirmation screen, maybe you could get the value inside the controller when you submit the form then get the form value, then you can return it in a twig again.

{% if month == '1' }Gennaio{% endif %}
{% if month == '2' }Febbraio{% endif %}

: : :

This is very naive, but I think at least for input, it is not so difficult to enter a month by digits. As suggested in the comments above, DatePicker would do that job in most cases.

Upvotes: 0

Related Questions