Asdra
Asdra

Reputation: 11

Symfony2, delimiters for a form

I noticed that when I generate a form with a builder like this:

$builder
        ->add('valide')
        ->add('adresse', new \Acme\CoreBundle\Form\AdresseType())
        ->add('prestations', 'entity', array(
            'class' => 'AcmeAppartBundle:Prestation',
            'property' => 'nom',
            'multiple' => true,
            'expanded' => true)
        );

The 'presations' field (which is a complete form) can be rendered with twig like that :{{ form(form.prestations) }}

My question is : is there a way to do the same for the rest of the form? I mean the same that : {{ form(form) }} But without the {{ form(form.prestations) }} part included in. (how to define 'sections' of a form in the formType)?

Upvotes: 0

Views: 74

Answers (2)

FyodorX
FyodorX

Reputation: 1480

If you need to change the render order so that form.presentations is rendered first and then the rest of the form, you can simply do this:

{{ form(form.prestations) }}
{{ form_rest(form) }}

When you do form_rest(form) it will render everything that hasn't been rendered before.

From the documentation:

This renders all fields that have not yet been rendered for the given form. It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you).

Upvotes: 1

dmnptr
dmnptr

Reputation: 4304

If you have parts of the form that you want to add/remove depending on certain conditions, you should use form events.

Take a look at the documentation here - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

Upvotes: 0

Related Questions