Skip to content
Advertisement

Symfony 2 dynamically change form based on a selected value

I have the following table:

Events:
 - Date (date)
 - EventType (varchar)
 - Value (varchar)

EventType can have a few values i.e check-in, checkout, room rent, misc, etc. I want to make a form that will change the field type of “Value” to text, date, or entity based on the selected type in “EventType”. I tried to find some solutions but didn’t succeed. The only thing I found is that I have to use queryBuilder but can’t understand how to apply it here.

Advertisement

Answer

You should use Event Subscribers, by adding an event subscriber to your Form, you delegate the creation of your fields to that Subscriber. Inside your Event Subscriber your could check your “EventType” and add the appropriate “Value” field.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement