storm
storm

Reputation: 409

Add dynamic input boxes in a Symfony form

What I want to do

I'm trying to build a form in Symfony 2.8 that requests the user's choice between many types of credentials and add an input box to the form it's similar to this one but I use a select option field instead of buttons.

What I did

I was able to create this form with HTML and JavaScript.The user can select more than one credential type only once except the OTV Option that can be selected without limits. image here

What's my problem

I need a way to store user's data in a session and get them in my controller. Is it possible to pass submitted data from HTML form to a Symfony controller and if not how can I build a similar form with Symfony's formBuilder ?

Upvotes: 0

Views: 1223

Answers (2)

user2959798
user2959798

Reputation:

CollectionType is probably what you are looking for.

For your purposes, you can think of it as adding rows to the end of a two-column list. The Left Column has credential_type with Name, Email etc in a <select>; and the Right Column allows for your text values.

You'll have two FormTypes: one for the list, and another for the list item. In the list item FormType, you should be able to set a CallbackConstraint to validate e.g. that email values are valid upon form submission.

All user changes are saved at the same time, only when the form as valid.

Upvotes: 1

Logans
Logans

Reputation: 119

I think you can do that. There is CollectionType in Symfony form. You can read about it here and how to implement it here

P.S. The main problem in this case is to generate specific names for dynamically created fields to provide a normal handling data in controller.

Upvotes: 0

Related Questions