Question

I have a very long form and would like to group some fields like this:

<input type="text" name"profile[name]">
<input type="text" name"formation[school_name]">

I read about collections but I don't know to do this using collections.

Was it helpful?

Solution

I don't have enough reputation to comment so I'll put some possibilities here as an answer.

If you just want to group your fields in the html then render each field on it's own in your twig template rather than rendering the entire form. You can read about that in the Symfony2 Form Documentation

If you need to render many forms with each one having the profile[name] and formation[school_name] then you likely want to use form collections. For example create a form type called Diego\MyBundle\Form\Type\MyNameAndSchoolType to hold your profile[name] and formation[school_name]. In the form that you display,

$builder->add('myNameAndSchool', 'collection', array(
                    'type'          => new \Diego\MyBundle\Form\Type\MyNameAndSchoolType(),
                    'label'         => 'myBundle.nameAndSchool.label',
                    'allow_add'     => true,
                    'allow_delete'  => true,
                    'prototype'     => true,
                    'by_reference'  => false
            ))

You read about form collections but for completeness here is the link Form Collections which also explains how to set up prototypes and some javascript examples for adding and removing records.

You may also want to check out MopaBootstrapBundle which has examples and helpers for handling collections including a javascript implementation.

I hope that helps a bit. I'm sure there are plenty of other ways to approach this as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top