Skip to content

Add BeforeFormValidateEvent for new and edit actions#7522

Open
Amoifr wants to merge 2 commits intoEasyCorp:5.xfrom
Amoifr:add_before_form_validate_event
Open

Add BeforeFormValidateEvent for new and edit actions#7522
Amoifr wants to merge 2 commits intoEasyCorp:5.xfrom
Amoifr:add_before_form_validate_event

Conversation

@Amoifr
Copy link
Copy Markdown

@Amoifr Amoifr commented Mar 25, 2026

Summary

Add a new BeforeFormValidateEvent dispatched after $form->handleRequest() but before $form->isValid().

Use cases:

  • Perform complex cross-field validation not easily handled by Constraints
  • Return a custom Response (e.g., for AJAX/Vue integrations)
  • Modify entity or form data dynamically before validation runs

Usage example:

use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeFormValidateEvent;

#[AsEventListener]
public function onBeforeFormValidate(BeforeFormValidateEvent $event): void
{
    $entity = $event->getEntityInstance();
    $form = $event->getForm();
    
    // Custom validation logic
    if ($someCondition) {
        $event->setResponse(new JsonResponse(['error' => 'Custom error']));
    }
}

Changes

  • New BeforeFormValidateEvent class extending AbstractLifecycleEvent with StoppableEventTrait
  • Event dispatched in AbstractCrudController::new() and edit() actions

Test plan

  • Manual testing with an event listener
  • Verify existing new/edit flows still work

Fix #7509

@pkly
Copy link
Copy Markdown
Contributor

pkly commented Apr 9, 2026

Would you consider adding events for after the form is submitted and validated while at it? #7254

There's a small use case for it where we add custom fields, but there's no easy way of reading it back.
Also - maybe consider passing AdminContext along to the event?

@Amoifr
Copy link
Copy Markdown
Author

Amoifr commented Apr 10, 2026

Thanks @pkly, both suggestions are spot on! Passing AdminContext and having a post-validation event would definitely make the feature more complete.

@javiereguiluz — what's your preference here? Should I keep this PR focused on BeforeFormValidateEvent and handle the rest in a follow-up (new issue/PR), or would you rather I bundle everything together?

Amoifr and others added 2 commits April 15, 2026 11:01
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 EasyCorp#7509
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Amoifr Amoifr force-pushed the add_before_form_validate_event branch from 346982e to b4add97 Compare April 15, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add BeforeFormValidateEvent to New/Edit Controllers

2 participants