From c37e841e61ef4606e355359a35ad419f16e890d9 Mon Sep 17 00:00:00 2001 From: Dragos Protung Date: Thu, 12 Nov 2026 20:40:39 +0100 Subject: [PATCH] Submit DTO forms that only post the save button, like EasyAdmin 5.10 --- composer.json | 2 +- config/phpstan.neon | 12 +++++++++ src/Controller/BaseCrudDtoController.php | 33 ++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 205ff45..11cb773 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,7 @@ "require-dev": { "carthage-software/mago": "^1.59.0", "doctrine/coding-standard": "^14.0.0", - "ergebnis/composer-normalize": "^2.56.0", + "ergebnis/composer-normalize": "^2.56.1", "php-standard-library/phpstan-extension": "^2.1.0", "phpstan/phpstan": "^2.3.5", "phpstan/phpstan-deprecation-rules": "^2.0.5", diff --git a/config/phpstan.neon b/config/phpstan.neon index 963b033..9f00c52 100644 --- a/config/phpstan.neon +++ b/config/phpstan.neon @@ -21,6 +21,18 @@ parameters: identifier: missingType.generics path: ../src/Action/ConfirmationModalActionsExtension.php count: 2 + # BaseCrudDtoController::handleFormRequest() is a byte-identical copy of EasyAdmin's, so it keeps EasyAdmin's + # untyped form and nested access to the posted data + - + message: '#^Method Protung\\EasyAdminPlusBundle\\Controller\\BaseCrudDtoController::handleFormRequest\(\) has parameter \$form with generic interface Symfony\\Component\\Form\\FormInterface but does not specify its types: TData$#' + identifier: missingType.generics + path: ../src/Controller/BaseCrudDtoController.php + count: 1 + - + message: "#^Cannot access offset 'btn' on mixed\\.$#" + identifier: offsetAccess.nonOffsetAccessible + path: ../src/Controller/BaseCrudDtoController.php + count: 1 includes: - phpstan-baseline.neon diff --git a/src/Controller/BaseCrudDtoController.php b/src/Controller/BaseCrudDtoController.php index 61e05a8..4848ee0 100644 --- a/src/Controller/BaseCrudDtoController.php +++ b/src/Controller/BaseCrudDtoController.php @@ -32,6 +32,8 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; @@ -97,6 +99,7 @@ abstract public function updateEntityFromDto(object $entity, object $dto): void; /** * This method should be identical to the parent edit() method except that: * - it uses updateEntityFromDto($originalEntity, $dto) to update the entity from the DTO. + * - it handles the form request like EasyAdmin 5.10 does, on every EasyAdmin version (see handleFormRequest()). */ #[Override] public function edit(AdminContext $context): KeyValueStore|Response @@ -158,7 +161,7 @@ public function edit(AdminContext $context): KeyValueStore|Response } $editForm = $this->createEditForm($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context); - $editForm->handleRequest($context->getRequest()); + $this->handleFormRequest($editForm, $context->getRequest()); if ($editForm->isSubmitted() && $editForm->isValid()) { $this->processUploadedFiles($editForm); @@ -197,6 +200,7 @@ public function edit(AdminContext $context): KeyValueStore|Response * This method should be identical to the parent new() method except that: * - it uses the createDto() method to create a new instance of the DTO instead of the entity. * - it uses createEntityFromDto($dto) to convert the DTO to the entity before persisting it. + * - it handles the form request like EasyAdmin 5.10 does, on every EasyAdmin version (see handleFormRequest()). * * In some scenarios, the entityId query param will get left in the generated urls when : * - you click around the admin @@ -228,7 +232,7 @@ public function new(AdminContext $context): KeyValueStore|Response $this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig()); $newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions(), $context); - $newForm->handleRequest($context->getRequest()); + $this->handleFormRequest($newForm, $context->getRequest()); /** @var TDto $entityInstance */ $entityInstance = $newForm->getData(); @@ -344,4 +348,29 @@ private function isGenerator(object $entityInstance): bool { return $entityInstance instanceof Generator; } + + // phpcs:disable + /** + * A byte-identical copy of the private AbstractCrudController::handleFormRequest() of EasyAdmin 5.10, so copy it + * again when EasyAdmin changes it. It is used on every EasyAdmin version: the buttons that save a form have the + * same name in all of them. phpstan.neon ignores the errors of its EasyAdmin code. + */ + private function handleFormRequest(FormInterface $form, Request $request): void + { + $form->handleRequest($request); + + // browsers don't send disabled fields nor unchecked checkboxes, so a form without a CSRF token + // and only those fields posts nothing under its name and Symfony doesn't consider it submitted; + // the button that saves the form is outside of it, so use it to detect the submission (see #7819) + if ($form->isSubmitted() || $request->getMethod() !== $form->getConfig()->getMethod()) { + return; + } + + if (null === ($request->request->all()['ea']['newForm']['btn'] ?? null)) { + return; + } + + $form->submit([], 'PATCH' !== $request->getMethod()); + } + // phpcs:enable }