tdbui22
tdbui22

Reputation: 347

Symfony embedded form has multiple buttons

I have 2 entities and I've created 2 separate form types for them. Each form type has a few fields and a submit button.

One of the entity is just a basic table (address) and the form type for this is used on a page independently and works as expected showing all its field and the submit button.

The other entity has a relation to the address table. The form type for this embeds the address form type along with a few of its own field and a submit button. The problem is this form type when displayed in the view shows 2 buttons which is not desired. And I would assume that if I embedded multiple form types it would show a button for each one of them too.

Is it possible to hide the button for the embedded form type so only the button in the current form type is shown?

Upvotes: 0

Views: 1307

Answers (2)

some_groceries
some_groceries

Reputation: 1192

A good way to do this is to remove the submit buttons from the form type entirely and add the submit buttons through the twig files, forms are meant to be reused most of the time, and in your case you are reusing them, it would't be fit to put them in the controller as you would be mixing presentation with controller logic, so the best place to put the submit button is in the twig. Best Practice

{{ form_start(form) }}
    {{ form_widget(form) }}

    <input type="submit" value="Create"
           class="btn btn-default pull-right" />
{{ form_end(form) }}

Upvotes: 5

DevDonkey
DevDonkey

Reputation: 4880

sure you can. Just use..

$form->remove('buttonName');

in your controller.


or, if you prefer to handle it in the formtype

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // ...
    if ($options['use_second_button']) {
        $builder->add('submit2', 'submit');
    }
    // ...
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'use_second_button' => true
    ));
}

and in the controller, pass in a false to turn it off

$form = $this-createForm(new someType(), $entity, ['use_second_button' => false]); 

heres a good resource.

Upvotes: 1

Related Questions