ardenexal/symfony-form-handler
Installation
composer require ardenexal/symfony-form-handler
(Note: Due to archival status, verify compatibility with your Symfony/Laravel environment via symfony/form and symfony/validator constraints.)
Register the Bundle
Add to config/bundles.php (Symfony) or manually bootstrap in Laravel via a service provider:
// app/Providers/AppServiceProvider.php (Laravel)
public function register()
{
$this->app->register(\Ardenexal\FormHandlerBundle\FormHandlerBundle::class);
}
First Use Case Create a form handler for a simple contact form:
// src/Form/ContactFormHandler.php
use Ardenexal\FormHandlerBundle\Form\FormHandlerInterface;
class ContactFormHandler implements FormHandlerInterface
{
public function handle(array $data)
{
// Process data (e.g., save to DB, send email)
return ['success' => true];
}
}
Controller Integration
// src/Controller/ContactController.php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Ardenexal\FormHandlerBundle\Form\FormHandler;
class ContactController
{
public function submit(Request $request, FormHandler $handler): Response
{
$data = $request->request->all();
$result = $handler->handle($data, ContactFormHandler::class);
return new Response(json_encode($result));
}
}
Form Handler Lifecycle
ValidatorInterface if a FormType is associated.handle() (e.g., entity persistence, API calls).['success' => true, 'message' => '...']).Dependency Injection Bind handlers as services for testability:
# config/services.yaml (Symfony)
services:
App\Form\ContactFormHandler:
tags: ['ardenexal.form_handler']
Form Integration
Pair with Symfony’s FormType for validation:
// src/Form/ContactType.php
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class);
$builder->add('email', EmailType::class);
}
}
// In handler
public function handle(array $data)
{
$form = $this->createForm(ContactType::class, null, ['data' => $data]);
if (!$form->isValid()) {
return ['errors' => $form->getErrors(true)];
}
// Proceed...
}
Laravel Adaptation
Symfony\Component\Form\FormFactory via Laravel’s Symfony bridge:
$factory = $this->app->make('form.factory');
$form = $factory->createNamedBuilder('contact', ContactType::class, null, ['data' => $data]);
Archival Status
symfony/form directly.Validation Overhead
handle() bypasses Symfony’s built-in form validation.FormHandlerInterface::validate() if available, or integrate ValidatorInterface:
$errors = $this->validator->validate($data, new ContactConstraints());
Laravel Compatibility
Request/Response; Laravel uses its own stack.$data = request()->all(); // Laravel
$response = response()->json($result); // Laravel
Handler Not Found
FormHandler.Validation Errors
$form->getErrors(true) to inspect constraints:
dd($form->getErrors(true)->getChildren());
Performance
handle(); offload to services (e.g., MessageBus for emails).Custom Handlers
Extend FormHandlerInterface to add pre/post-processing:
class CustomFormHandler implements FormHandlerInterface
{
public function handle(array $data)
{
$this->preProcess($data);
$result = parent::handle($data);
$this->postProcess($result);
return $result;
}
}
Event Listeners
Attach listeners to FormHandlerEvents (if supported) for cross-cutting concerns:
$dispatcher->addListener('form.handler.process', function ($event) {
// Log, audit, etc.
});
Testing
Mock FormHandler in PHPUnit:
$handler = $this->createMock(FormHandler::class);
$handler->method('handle')->willReturn(['success' => true]);
How can I help you explore Laravel packages today?