Skip to content

How to Handle Multiple Submit Buttons with Turbo Drive

Edit this page

When your form contains more than one submit button and you want to check which of the buttons was clicked to adapt the program flow in your controller, you need to add a value to each button because Turbo Drive doesn't send elements with an empty value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// src/Form/TaskType.php
namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            // ...
            ->add('save', SubmitType::class, [
                'label' => 'Create Task',
                'attr' => [
                    'value' => 'create-task',
                ],
            ])
            ->add('saveAndAdd', SubmitType::class, [
                'label' => 'Save and Add',
                'attr' => [
                    'value' => 'save-and-add',
                ],
            ]);
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version