Skip to content

Commit 0177ea9

Browse files
committed
Add BeforeFormValidateEvent for new and edit actions
Add a new event dispatched after form submission but before validation. This allows developers to: - Perform complex cross-field validation - Return a custom Response (e.g., for AJAX integrations) - Modify entity or form data before validation runs The event is dispatched in both new() and edit() actions. Fix #7509
1 parent 640e870 commit 0177ea9

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/Controller/AbstractCrudController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
3434
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
3535
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
36+
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeFormValidateEvent;
3637
use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
3738
use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
3839
use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
@@ -263,6 +264,15 @@ public function edit(AdminContext $context): KeyValueStore|Response
263264

264265
$editForm = $this->createEditForm($context->getEntity(), $context->getCrud()->getEditFormOptions(), $context);
265266
$editForm->handleRequest($context->getRequest());
267+
268+
if ($editForm->isSubmitted()) {
269+
$event = new BeforeFormValidateEvent($entityInstance, $editForm);
270+
$this->container->get('event_dispatcher')->dispatch($event);
271+
if ($event->isPropagationStopped()) {
272+
return $event->getResponse();
273+
}
274+
}
275+
266276
if ($editForm->isSubmitted() && $editForm->isValid()) {
267277
$this->processUploadedFiles($editForm);
268278

@@ -327,6 +337,14 @@ public function new(AdminContext $context): KeyValueStore|Response
327337
$entityInstance = $newForm->getData();
328338
$context->getEntity()->setInstance($entityInstance);
329339

340+
if ($newForm->isSubmitted()) {
341+
$event = new BeforeFormValidateEvent($entityInstance, $newForm);
342+
$this->container->get('event_dispatcher')->dispatch($event);
343+
if ($event->isPropagationStopped()) {
344+
return $event->getResponse();
345+
}
346+
}
347+
330348
if ($newForm->isSubmitted() && $newForm->isValid()) {
331349
$this->processUploadedFiles($newForm);
332350

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Event;
4+
5+
use Symfony\Component\Form\FormInterface;
6+
7+
/**
8+
* Event dispatched after form submission but before validation.
9+
* This allows to perform custom validation, modify data, or return
10+
* a custom response before the standard validation runs.
11+
*
12+
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
13+
*
14+
* @template TEntity of object
15+
*
16+
* @extends AbstractLifecycleEvent<TEntity>
17+
*/
18+
final class BeforeFormValidateEvent extends AbstractLifecycleEvent
19+
{
20+
use StoppableEventTrait;
21+
22+
/**
23+
* @param TEntity $entityInstance
24+
*/
25+
public function __construct(
26+
protected object $entityInstance,
27+
private readonly FormInterface $form,
28+
) {
29+
}
30+
31+
public function getForm(): FormInterface
32+
{
33+
return $this->form;
34+
}
35+
}

0 commit comments

Comments
 (0)