Installation
composer require chaplean/form-handler-bundle
Add the bundle to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Chaplean\FormHandlerBundle\ChapleanFormHandlerBundle::class => ['all' => true],
First Use Case
Inject the FormHandler service into a controller and use it to handle form submissions:
use Symfony\Component\HttpFoundation\Request;
public function createAction(Request $request)
{
return $this->get('chaplean_form_handler.form_handler')
->handle(UserType::class, new User(), $request);
}
Key Files to Review
src/Chaplean/FormHandlerBundle/DependencyInjection/Configuration.php (for config options).src/Chaplean/FormHandlerBundle/Form/FormHandler.php (core logic).src/Chaplean/FormHandlerBundle/Resources/config/services.xml (service definitions).Form Handling
Use handle() to process requests with a form type and entity:
$handler = $this->get('chaplean_form_handler.form_handler');
$response = $handler->handle(
FormType::class, // Form type class
$entity, // Entity instance (new or existing)
$request, // Request object
['group' => 'default'] // Optional options
);
Integration with FOSRestBundle Leverage annotations for RESTful endpoints:
use FOS\RestBundle\Controller\Annotations\RouteResource;
/**
* @RouteResource("User")
*/
class UserController extends FOSRestController
{
public function postAction(Request $request)
{
return $this->get('chaplean_form_handler.form_handler')
->handle(UserType::class, new User(), $request);
}
}
Customizing Responses Override default serialization groups or error formats:
$handler->handle(
UserType::class,
$user,
$request,
[
'serialization_groups' => ['public'],
'error_format' => 'api_platform'
]
);
Event Listeners Attach listeners to modify behavior (e.g., pre/post-processing):
# config/services.yaml
services:
App\EventListener\CustomFormListener:
tags:
- { name: chaplean_form_handler.listener }
handle() for create/update logic.serialization_groups).Missing Form Type
Ensure the form type class (e.g., UserType) is registered with Symfony’s form system. Use:
$builder->add('name', TextType::class);
Entity Not Persisted By default, the handler does not persist entities. Manually flush or configure auto-persist:
$handler->handle(..., $request, ['auto_persist' => true]);
Serialization Groups
If responses are empty, verify serialization_groups are defined in your entity’s @Groups annotations.
Request Binding
The handler expects Request objects with JSON/XML payloads. For HTML forms, use Request::get() or Request::request.
Enable Form Debugging
Add to config/packages/dev/form.yaml:
framework:
form: { enabled: true }
Check Listener Order
If listeners conflict, adjust their priority in the tags configuration.
Custom Error Formats
Override the default error formatter by implementing Chaplean\FormHandlerBundle\Error\ErrorFormatterInterface.
Pre/Post-Processing
Use event listeners (chaplean_form_handler.listener) to:
Configuration
Override default settings in config/packages/chaplean_form_handler.yaml:
chaplean_form_handler:
default_options:
auto_persist: false
serialization_groups: ['default']
Testing
Mock the FormHandler service in tests:
$handler = $this->createMock(FormHandler::class);
$handler->method('handle')->willReturn($response);
$this->container->set('chaplean_form_handler.form_handler', $handler);
How can I help you explore Laravel packages today?