Developed with love by KnpLabs Hire us for your project!
33

form-handler-bundle

by hostnet

Contains Symfony2 glue for the component

form-handler-bundle

The form handler bundle is designed give extra support on the form handler component. This includes predefined services like the SimpleFormProvider and a handy ParamConverter to easily inject your form handler into a controller.

This bundle wraps itself around the form-handler-component which in turn, wraps itself around a form. It provides a small interface that you can use to handle the forms by sending through the Request and FormHandlerInterface. This interface requires you to implement a few methods. By adding 2 more optional interfaces; FormFailureHandlerInterface and FormSuccessHandlerInterface, you can move your success and failure branches away.

Moving away your success and failure branches will cause the following:
- Less dependencies in your controller
- Re-usable code
- Makes it easier to unit-test the results
- A handler is "allowed" to have an entity manager as opposing to your service (because you don't want to randomly call flush, it's expensive).

In the examples provided below, you can see a controller and a handler that uses a parameter converter. If the pre-provided SimpleFormProvider isn't enough, you can always implement your own variant by using the FormProviderInterface, both found in the component (the service definition itself is in the bundle).

Installation

In your composer.json
json
{
"require" : {
"hostnet/form-handler-bundle" : "master@dev"
}
}

Note: Recommended is to use the current stable tag.

Then add the bundle in your AppKernel:
php
$bundles = [
// ...
new Hostnet\Bundle\FormHandlerBundle\FormHandlerBundle()
];

Usage

In order to use the form handler, simply create a service that contains your form information. A simple example would be:
```php
use Hostnet\Component\Form\FormFailureHandlerInterface;
use Hostnet\Component\Form\FormHandlerInterface;
use Hostnet\Component\Form\FormSuccesHandlerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;

class MyFormHandler implements FormHandlerInterface, FormSuccesHandlerInterface, FormFailureHandlerInterface
{
private $data;
private $form;
private $router;
private $user;

public function __construct(RouterInterface $router)
{
    $this->data   = new MyFormData();
    $this->router = $router;
}

public function setUser(MyEntityUser $user)
{
    $this->user = $user;
}

public function getType()
{
    return 'my_form'; // form.type alias, as tagged in your services.yml
}

public function getData()
{
    return $this->data;
}

public function getOptions()
{
    return [];
}

public function getForm()
{
    return $this->form;
}

public function setForm(FormInterface $form)
{
    $this->form = $form;
}

public function onSuccess(Request $request)
{
    // do something with the form data, like setting some data in the user
    $user->setUsername($this->data->getUsername());

    // ...
    return new RedirectResponse($this->router->generate("my-route"));
}

public function onFailure(Request $request)
{
    // log the failed form post, or create a custom redirect.
}

}
```

Note: Implementing the FormSuccesHandlerInterface and FormFailureHandlerInterface is optional and in most cases you will not need the FormFailureHandlerInterface since you will want to render the page again but with the form errors.

Then create a service and tag it with form.handler
yaml
my_form.handler:
class: MyFormHandler
arguments:
- "@router"
tags:
- { name: form.handler }

And in your controller you can use the handler like:
```php
class MyController
{
private $form_provider;

/**
 * @param SimpleFormProvider $form_provider form_handler.provider.simple 
 */
public function __construct(SimpleFormProvider $form_provider)
{
    $this->form_provider = $form_provider;
}

/**
 * @Route("/your-route", name="route-name")
 * @Template()
*/
public function formAction(
    Request       $request,
    MyFormHandler $handler,
    MyEntityUser  $user
) {
    // you have 100% control over you handler, so you can
    // create setters that allow you to "inject" more things
    $handler->setUser($user);
    if (($response = $this->form_provider->handle($request, $handler)) instanceof RedirectResponse) {
        return $response;
    }
    // regular or in-valid flow
    return [
        'form' => $handler->getForm()->createView()
    ];
}

}
```
The parameter converter will fetch the correct service with the class based on the tagged services.

Note: You cannot have multiple form handler services that use the same class, since the parameter converter cannot find the correct service for it.

Copyright (c) 2014 Hostnetbv

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
  • Merge pull request #9 from iltar/feature/license-fix
    By iltar, 2 years ago
  • Fixed the license
    By iltar, 2 years ago
  • Merge pull request #8 from jrobeson/trivial-readme-fixes
    By iltar, 2 years ago
  • minor README fixes
    By jrobeson, 2 years ago
  • Merge pull request #6 from iltar/master
    By yannickl88, 3 years ago
  • Updated readme
    By iltar, 3 years ago
  • Merge pull request #5 from iltar/master
    By yannickl88, 3 years ago
  • Merge pull request #1 from iltar/scrutinizer-patch-1
    By iltar, 3 years ago
  • Scrutinizer Auto-Fixes
    By scrutinizer-auto-fixer, 3 years ago
  • Keeping Scrutinizer happy
    By iltar, 3 years ago
  • Moved stuff around, fixes based on scrutinizer, added unit-test
    By iltar, 3 years ago
  • Updated a Major Dependency
    By iltar, 3 years ago
  • Merge pull request #4 from yannickl88/master
    By iltar, 3 years ago
  • Removed form factory from services.yml
    By yannickl88, 3 years ago
  • Removed form creation from the param convertor
    By yannickl88, 3 years ago
  • Update README.md
    By iltar, 3 years ago
  • Updated license for public usage and added component version
    By iltar, 3 years ago
  • Merge pull request #3 from yannickl88/master
    By iltar, 3 years ago
  • Merge remote-tracking branch 'upstream/master'
    By yannickl88, 3 years ago
  • Added travis support
    By yannickl88, 3 years ago
  • Update README.md
    By yannickl88, 3 years ago
  • Update README.md
    By yannickl88, 3 years ago
  • Update README.md
    By yannickl88, 3 years ago
  • Added more explanation to describe the purpose
    By iltar, 3 years ago
  • Update README.md
    By iltar, 3 years ago
  • Merge pull request #2 from yannickl88/master
    By yannickl88, 3 years ago
  • Fixed PSR coding style
    By yannickl88, 3 years ago
  • Update README.md
    By yannickl88, 3 years ago
  • Update README.md
    By yannickl88, 3 years ago
  • Added unit-tests
    By iltar, 3 years ago