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 7.1 doesn't send the clicked button when it has no value attribute:

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',
                ],
            ]);
    }
}

Note

This is only needed with Turbo 7.1. Starting from Turbo 7.2, the clicked button is always sent, and the isClicked() method of each button works without adding any value.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version