Installation
Add the bundle to your composer.json:
composer require byscripts/form-handler-bundle
Enable it in config/bundles.php (Symfony 4+):
return [
// ...
Byscripts\FormHandlerBundle\ByscriptsFormHandlerBundle::class => ['all' => true],
];
Basic Setup
use Byscripts\FormHandlerBundle\Annotation\FormHandler;
class MyController extends AbstractController
{
/**
* @FormHandler("my_form_type")
*/
public function handleForm(Request $request)
{
// Logic here
}
}
MyFormType) extending AbstractType.First Use Case Submit a form and let the bundle handle validation/processing:
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit">Submit</button>
{{ form_end(form) }}
The @FormHandler annotation routes submissions to handleForm().
Annotation-Driven Routing
@FormHandler("form_type_name") to bind a form type to a handler method./**
* @FormHandler("user_registration")
*/
public function registerUser(Request $request, UserRegistrationForm $form)
{
$data = $form->handle($request->request->all());
// Process $data (e.g., save to DB)
}
Form Type Configuration
AbstractType and define fields:
class UserRegistrationForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('password', RepeatedType::class);
}
}
Validation & Processing
$form->handle().public function handleForm(Request $request, UserRegistrationForm $form)
{
$data = $form->handle($request->request->all());
if ($data['valid']) {
$user = new User($data['data']);
$entityManager->persist($user);
}
return $this->redirectToRoute('home');
}
CSRF & Security
{{ form_start(form) }} includes CSRF protection (default in Symfony forms).form_start(form, {'attr': {'id': 'api-form'}}) and validate manually.Deprecated/Obsolete Code
Annotation Overhead
@FormHandler is annotation-based; if you prefer attributes (PHP 8+), you’ll need to extend the bundle.Validation Quirks
ValidatorInterface. For custom validation, inject it manually:
public function __construct(private ValidatorInterface $validator) {}
No Laravel Port
@FormHandler routes are registered. Override routing if needed:
# config/routes.yaml
_form_handler:
resource: "@ByscriptsFormHandlerBundle/Resources/config/routing.yml"
$form->getErrors() in your handler to debug:
var_dump($form->getErrors()->getIterator());
{{ form_start(form) }} is used in templates (not manual HTML).Byscripts\FormHandlerBundle\Handler\FormHandlerInterface for reusable logic.form.handler.submit events (if the bundle supports them).// app/Http/Middleware/HandleFormSubmission.php
public function handle(Request $request, Closure $next)
{
if ($request->isMethod('post') && $request->has('form_type')) {
$handler = app()->make("form.handler.{$request->input('form_type')}");
return $handler->process($request);
}
return $next($request);
}
How can I help you explore Laravel packages today?