, (*1)
The FormHandler component attempts to make controllers with basic form handlers cleaner by off-loading form handling to separate classes., (*2)
Table of Contents
Requirements
FormHandler requires PHP 7.1+ and Symfony 3.0+, (*3)
Installation
Composer
$ composer require solidworx/form-handler-bundle:^1.0
Then register the bundle in your Symfony application:, (*4)
<?php
// app/AppKernel.php
// ...
public function registerBundles()
{
$bundles = [
// ...
new SolidWorx\FormHandler\FormHandlerBundle(),
];
// ...
)
Usage
A form can have a class that implements the FormHandlerInterface
interface. This interface exposes a single method in which the form can be retrieved:, (*5)
public function getForm(FormFactoryInterface $factory, Options $options);
This method can either return a standard form type, or use the factory to generate a form type., (*6)
<?php
use Symfony\Component\Form\FormFactoryInterface;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\Options;
class MyFormHandler implements FormHandlerInterface
{
public function getForm(FormFactoryInterface $factory, Options $options)
{
// either
return MyFormType::class;
// or
return $factory->create(MyFormType::class);
}
}
The benefit of using the factory, is when you need to pass additional information or options to the form, E.G, (*7)
return $factory->create(MyFormType::class, null, ['horizontal' => true]);
To register your form handler, register it as a service:, (*8)
services:
my.form.handler:
class: MyFormHandler
tags:
- { name: 'form.handler' }
Inside your controller, use the form.handler
service to handle your form:, (*9)
<?php
class MyController extends Controller
{
public function addAction()
{
return $this->get('solidworx.form_handler')->handle(MyFormHandler::class); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
}
}
This will process the necessary logic on the form (submit the form and handle the request etc)., (*10)
If you need to handle a failed form, you need to implement the FormHandlerFailInterface
interface:, (*11)
<?php
use SolidWorx\FormHandler\FormRequest;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerFailInterface;
use Symfony\Component\Form\FormErrorIterator;
class MyFormHandler implements FormHandlerInterface, FormHandlerFailInterface
{
// ...
public function onFail(FormRequest $formRequest, FormErrorIterator $errors, $data = null)
{
// Form submission has failed, probably due to a validation error.
// Handle it here if you need specific custom logic
}
}
If you need to handle a successful form submission, implement the FormHandlerSuccessInterface
interface:, (*12)
<?php
use SolidWorx\FormHandler\FormRequest;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerSuccessInterface;
class MyFormHandler implements FormHandlerInterface, FormHandlerSuccessInterface
{
// ...
public function onSuccess($data, FormRequest $form)
{
// $data is the submitted data from the form, do something with it here
// This will probably save info to the DB
}
}
If you need to pass options to a form, you can add it as an array to the second argument of FormHandler::handle
:, (*13)
<?php
class MyController extends Controller
{
public function addAction()
{
return $this->get('solidworx.form_handler')->handle(MyFormHandler::class, ['entity' => new Blog]); // MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler`
}
}
The options will then be available in the getForm
method as a Options
object:, (*14)
<?php
use Symfony\Component\Form\FormFactoryInterface;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\Options;
class MyFormHandler implements FormHandlerInterface
{
public function getForm(FormFactoryInterface $factory, Options $options)
{
return $factory->create(MyFormType::class, $options->get('entity'));
}
}
You can also configure the options to set what options is allowed, set default values, define required options etc. by implementing the FormHandlerOptionsResolver
interface:, (*15)
<?php
use Symfony\Component\OptionsResolver\OptionsResolver;
use SolidWorx\FormHandler\FormHandlerInterface;
use SolidWorx\FormHandler\FormHandlerOptionsResolver;
class MyFormHandler implements FormHandlerInterface, FormHandlerOptionsResolver
{
// ...
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('entity');
$resolver->addAllowedTypes('entity', BlogEntity::class);
$resolver->setDefault('another_options', 'myvalue');
}
}
Advanced Usage
That is the very basics of the component. There are more advanced usages where you can customize the handling of a form to your specific needs., (*16)
Testing
To run the unit tests, execute the following command, (*17)
$ vendor/bin/phpunit
Contributing
See CONTRIBUTING, (*18)
License
FormHandler is open-sourced software licensed under the MIT license, (*19)
Please see the LICENSE file for the full license., (*20)