I want to group fields in symfony2. For example wrap them in a div and place headlines in between:
<form>
<div class="step-1">
<h3>Step 1</h3>
Field 1
Field 2
</div>
<div class="step-2">
<h3>Step 2</h3>
Field 3
Field 4
</div>
</form>
The problem is the form got very much fields so i cant render them one by one in the template. Isnt there any option when adding fields? Like:
$form = $this->createFormBuilder()
->addGroup('step-1')
Or how can i handle this?
The structure in your form class shouldn't necessarily be related to the structure in your layout. You can structure the fields in yout HTML in whichever way you like. In your case you would put your step headings just like you did in your Q, e.g.:
If you still interested to group forms (I did not test that):
Source.
I found out, according to this post(Thanks "n.1" for the link) it is possible to group within controller:
Which gives following template:
Which i can theme the way i want to. But also "bschussek" wrote:
So maybe the best practice is not to use the controller for structuring and I should prefer the practice from "n.1"
Twig can help you to display multiple fields with a minimal code: