diff --git a/src/Controller/AbstractCrudController.php b/src/Controller/AbstractCrudController.php index 373b588f26..79bbef62f6 100644 --- a/src/Controller/AbstractCrudController.php +++ b/src/Controller/AbstractCrudController.php @@ -33,6 +33,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent; +use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeFormValidateEvent; use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException; use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException; use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException; @@ -263,6 +264,15 @@ public function edit(AdminContext $context): KeyValueStore|Response $editForm = $this->createEditForm($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context); $editForm->handleRequest($context->getRequest()); + + if ($editForm->isSubmitted()) { + $event = new BeforeFormValidateEvent($entityInstance, $editForm); + $this->container->get('event_dispatcher')->dispatch($event); + if ($event->isPropagationStopped()) { + return $event->getResponse(); + } + } + if ($editForm->isSubmitted() && $editForm->isValid()) { $this->processUploadedFiles($editForm); @@ -327,6 +337,14 @@ public function new(AdminContext $context): KeyValueStore|Response $entityInstance = $newForm->getData(); $context->getEntity()->setInstance($entityInstance); + if ($newForm->isSubmitted()) { + $event = new BeforeFormValidateEvent($entityInstance, $newForm); + $this->container->get('event_dispatcher')->dispatch($event); + if ($event->isPropagationStopped()) { + return $event->getResponse(); + } + } + if ($newForm->isSubmitted() && $newForm->isValid()) { $this->processUploadedFiles($newForm); diff --git a/src/Event/BeforeFormValidateEvent.php b/src/Event/BeforeFormValidateEvent.php new file mode 100644 index 0000000000..e7d78e4be3 --- /dev/null +++ b/src/Event/BeforeFormValidateEvent.php @@ -0,0 +1,36 @@ + + * + * @template TEntity of object + * + * @extends AbstractLifecycleEvent + */ +final class BeforeFormValidateEvent extends AbstractLifecycleEvent +{ + use StoppableEventTrait; + + /** + * @param TEntity $entityInstance + */ + public function __construct( + object $entityInstance, + private readonly FormInterface $form, + ) { + parent::__construct($entityInstance); + } + + public function getForm(): FormInterface + { + return $this->form; + } +}