Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions config/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions src/Controller/BaseCrudDtoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
Loading