Skip to content

Add BeforeFormValidateEvent to New/Edit Controllers #7509

@ucscode

Description

@ucscode

Description

Currently, EasyAdmin provides hooks for events after an entity is persisted or updated. However, there is no clean way to intercept the lifecycle after a form is submitted but before $form->isValid() is called.

Adding a BeforeFormValidateEvent would allow developers to:

  1. Perform complex cross-field validation that isn't easily handled by standard Constraints.
  2. Stop the validation process and return a custom Response (e.g., for AJAX/Vue integrations).
  3. Modify the entity or form data dynamically based on the raw submission before the validation engine runs.

Proposed Workflow

In AdminContext or the AbstractCrudController, the logic would look like this:

$form->handleRequest($request);
if ($form->isSubmitted()) {
    $event = new BeforeFormValidateEvent($entityInstance, $form);
    $this->container->get('event_dispatcher')->dispatch($event);

    if ($event->isPropagationStopped()) {
        return $event->getResponse();
    }

    if ($form->isValid()) {
        // ... standard persistence logic
    }
}

Potential Implementation

1. The Event Class:
A new BeforeFormValidateEvent class that holds the entityInstance, the form, and a setter for a response.

2. The Controller Integration:
Update EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController in the new() and edit() actions to dispatch this event immediately after handleRequest().

Why this is needed

In modern decoupled setups (like using Vue.js or Stimulus within EasyAdmin), we often need to validate credentials or external API states before Symfony attempts to map the request to the database. Without this event, we are forced to override the entire new() or edit() methods just to insert a few lines of validation logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions