Skip to content

When using ux-turbo, FormInterface is unabled to detect which button is clicked #95

@Alexandre-T

Description

@Alexandre-T

Symfony version(s) affected: 5.3+

Description
When UX-TURBO is activated,
$form->get('submit')->isClicked is always returning false
$form->getClickedButton() is always returning null

How to reproduce

  1. Start a full skeleton symfony project,
  2. Create a controller with a form and multiple buttons
  3. In your controller, dump the content $form->getClickedButton, all is working.
  4. Install symfony/ux-turbo
  5. Retry, the content of $form->getClickedButton is now null

Below, all steps are detailed.

symfony new --full issue-ux-turbo
cd issue-ux-turbo
symfony console make:controller Home

Open src/Controller/HomeController and edit it:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class HomeController extends AbstractController
{
    #[Route('/home', name: 'home')]
    public function index(Request $request): Response
    {
        $form = $this->createFormBuilder()
            ->add('task', TextType::class, ['required' => false])
            ->add('save', SubmitType::class, ['label' => 'SAVE'])
            ->add('saveAndAdd', SubmitType::class, ['label' => 'Save and Add'])
            ->getForm();

        $form->handleRequest($request);

        $clicked = "Form wasn't submitted";
        $code = 200;

        if ($form->isSubmitted() && $form->isValid()) {
            $button = $form->getClickedButton();
            if ($button instanceof FormInterface) {
                $clicked = $button->getName();
            } else {
                $clicked = serialize($button); //will return N; (button is null when turbo is running)
            }
            $code = 422;
        }

        $response = new Response(null, $code);

        return $this->render('home/index.html.twig', [
            'clicked' => $clicked,
            'form' => $form->createView(),
            'controller_name' => 'HomeController',
        ], $response);
    }
}

Open templates/home/index.html.twig and edit it to add clicked and form:

{% extends 'base.html.twig' %}

{% block title %}Hello HomeController!{% endblock %}

{% block body %}
<style>
    .example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
    .example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>

<div class="example-wrapper">
    <h1>Hello {{ controller_name }}! ✅</h1>

    {{ form(form) }}
    This friendly message is coming from:
    <ul>
        <li>Your controller at <code><a href="{{ '/home/alexandre/PhpstormProjects/issue-ux-turbo/src/Controller/HomeController.php'|file_link(0) }}">src/Controller/HomeController.php</a></code></li>
        <li>Your template at <code><a href="{{ '/home/alexandre/PhpstormProjects/issue-ux-turbo/templates/home/index.html.twig'|file_link(0) }}">templates/home/index.html.twig</a></code></li>
        <li>Clicked button <code>{{ clicked }}</code></li>
    </ul>
</div>
{% endblock %}

Test it, no issues yet.

Install symfony/ux-turbo

symfony composer require symfony/ux-turbo
yarn install --force
yarn run encore dev

Uncomment the Twig helpers in templates/base.html.twig

Test it, the bug appears !
Disable Javascript, the bug disappears, of course.

Possible Solution
This is perhaps a Turbo bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions