SymfonyLive San Francisco 2012 - Bernhard Schussek - Symfony2 Form Tricks

Symfony · 9K views · 2012 · 41:26

Open on YouTube

Web developmentSoftware architecture & designtalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering Symfony2 form questions about dynamic fields, form events, data transformers and custom choice types.

Recommended by 3 people across 3 different months, 2012 to 2014.

What it is

A 41-minute SymfonyLive San Francisco 2012 conference talk by Bernhard Schussek, a core Symfony Form component developer, on advanced Symfony2 form techniques. It covers form types as descriptions rather than data holders, form events, data transformers and custom types.

What people said

Recommends Ziumin · 2012-11 · Stack Overflow · on “Choices as choice option in choice field Type in symfony2”

No. There is no such functionality bundled in the standart form component. The best way of doing this is to create custom form type [LINK] + some JS code to show/hide necessary fields. And i suggest you to watch this video [this video] (there is an example of similiar problem with choice and optional text).

Recommends Ahmed Siouani · 2013-02 · Stack Overflow · on “Symfony2: adding form fields dynamically using information from request”

You don't have to setData() on the $event object. You've to implement an on POST_SET_DATA [LINK] logic that builds your form the way you want [LINK] according to you data. You've then to initialize your form (inside your controller) using your webservice's reply. ( Note: SET_DATA form event is deprecated since version 2.1. It'll be definitely removed in version 2.3) Update: You can set an array as form data and use a DataTransformer to structure your form's data the way you want. Take a look at the Data Transformation part [this video] of Symfony 2 Form Tricks [this video] during the last San Francisco Symfony Live.

Recommends Loial · 2013-12 · Stack Overflow · on “symfony2 - adding choices from database”

I can't comment or downvote yet, so I'll just reply to Qoop's answer here: What you proposed will work unless you start using your form type class as a service. You should generally avoid adding data to your form type object through constructor. Think of form type class like a Class - it's a kind of description of your form. When you make an instance of form (by building it) you get the Object of form that is build by the description in form type and then filled with data. Take a look at this: [this video] - this situation is described around 31 minute of the presentaion. You should use form event FormEvents::PRE_SET_DATA and manipulate fields when the form is injected with data. See: [LINK]

Cites Leevi Graham · 2014-01 · Stack Overflow · on “Symfony2 DateRange custom type”

I'm trying to create a simple custom Symfony2 dateRange formType with a slight twist. The idea is that the user can choose a string representation of the date eg "today", "this month" or "custom" start / end dates. When the form is submitted if a user chooses a period eg: "today" the start and end submitted form data are ignored and the start and end are calculated based on the period. When submitting the form with "custom" period: When I fill in the form And submit "period" with "custom" And submit "start" with "2014-01-01" And submit "end" with "2014-01-01" Then the form should display: "custom" in the period select box And "2014-01-01" in start And "2014-01-01" in end When submitting the