- Notifications
You must be signed in to change notification settings - Fork1
Easy form handling for Symfony forms
License
SolidWorx/FormHandlerBundle
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The FormHandler component attempts to make controllers with basic form handlers cleaner by off-loading form handling to separate classes.
FormHandler requires PHP 7.1+ and Symfony 3.0+
$ composer require solidworx/form-handler-bundle:^1.0
Then register the bundle in your Symfony application:
<?php// app/AppKernel.php// ...publicfunction registerBundles() {$bundles = [// ...newSolidWorx\FormHandler\FormHandlerBundle(), ];// ... )
A form can have a class that implements theFormHandlerInterface
interface. This interface exposes a single method in which the form can be retrieved:
publicfunction getForm(FormFactoryInterface$factory,Options$options);
This method can either return a standard form type, or use the factory to generate a form type.
<?phpuseSymfony\Component\Form\FormFactoryInterface;useSolidWorx\FormHandler\FormHandlerInterface;useSolidWorx\FormHandler\Options;class MyFormHandlerimplements FormHandlerInterface{publicfunctiongetForm(FormFactoryInterface$factory,Options$options) {// eitherreturn MyFormType::class;// orreturn$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
return$factory->create(MyFormType::class,null, ['horizontal' =>true]);
To register your form handler, register it as a service:
services:my.form.handler:class:MyFormHandlertags: -{ name: 'form.handler' }
Inside your controller, use theform.handler
service to handle your form:
<?phpclass MyControllerextends Controller{publicfunctionaddAction() {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).
If you need to handle a failed form, you need to implement theFormHandlerFailInterface
interface:
<?phpuseSolidWorx\FormHandler\FormRequest;useSolidWorx\FormHandler\FormHandlerInterface;useSolidWorx\FormHandler\FormHandlerFailInterface;useSymfony\Component\Form\FormErrorIterator;class MyFormHandlerimplements FormHandlerInterface, FormHandlerFailInterface{// ...publicfunctiononFail(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 theFormHandlerSuccessInterface
interface:
<?phpuseSolidWorx\FormHandler\FormRequest;useSolidWorx\FormHandler\FormHandlerInterface;useSolidWorx\FormHandler\FormHandlerSuccessInterface;class MyFormHandlerimplements FormHandlerInterface, FormHandlerSuccessInterface{// ...publicfunctiononSuccess($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 ofFormHandler::handle
:
<?phpclass MyControllerextends Controller{publicfunctionaddAction() {return$this->get('solidworx.form_handler')->handle(MyFormHandler::class, ['entity' =>newBlog]);// MyFormHandler will automatically be pulled from the container if it is tagges with `form.handler` }}
The options will then be available in thegetForm
method as aOptions
object:
<?phpuseSymfony\Component\Form\FormFactoryInterface;useSolidWorx\FormHandler\FormHandlerInterface;useSolidWorx\FormHandler\Options;class MyFormHandlerimplements FormHandlerInterface{publicfunctiongetForm(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 theFormHandlerOptionsResolver
interface:
<?phpuseSymfony\Component\OptionsResolver\OptionsResolver;useSolidWorx\FormHandler\FormHandlerInterface;useSolidWorx\FormHandler\FormHandlerOptionsResolver;class MyFormHandlerimplements FormHandlerInterface, FormHandlerOptionsResolver{// ...publicfunctionconfigureOptions(OptionsResolver$resolver):void {$resolver->setRequired('entity');$resolver->addAllowedTypes('entity', BlogEntity::class);$resolver->setDefault('another_options','myvalue'); }}
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.
To run the unit tests, execute the following command
$ vendor/bin/phpunit
SeeCONTRIBUTING
FormHandler is open-sourced software licensed under theMIT license
Please see theLICENSE file for the full license.
About
Easy form handling for Symfony forms
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.