Skip to content
Open
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
18 changes: 18 additions & 0 deletions src/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

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

Expand Down
36 changes: 36 additions & 0 deletions src/Event/BeforeFormValidateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Event;

use Symfony\Component\Form\FormInterface;

/**
* Event dispatched after form submission but before validation.
* This allows to perform custom validation, modify data, or return
* a custom response before the standard validation runs.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*
* @template TEntity of object
*
* @extends AbstractLifecycleEvent<TEntity>
*/
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;
}
}
Loading