0

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 ?

2
  • take a look at symfony.com/doc/current/cookbook/form/… Commented Feb 18, 2016 at 9:41
  • Thanks I'm giving this a try but I want to know first if there is any way to transmit data from a HTML form can I save them in a random object in twig and then get them in the controller ? Commented Feb 18, 2016 at 10:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this is a part of the solution but how can I dynamically generate input boxes and respect the conditions ( zero or one selection for every option and zero or more for OTV )
The list FormType will create a HTML prototype which will appear when the user clicks "Add" or similar - the cookbook article describes this.
You can check you have all the items you need in your controller when the form is Valid, and then make it invalid if things are missing
0

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.

1 Comment

Thanks.. this may be a part of the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.