2017 © Pedro Peláez
 

symfony-bundle form-handler-bundle

Easy form handling for Symfony forms

image

solidworx/form-handler-bundle

Easy form handling for Symfony forms

  • Friday, May 11, 2018
  • by pierredup
  • Repository
  • 1 Watchers
  • 4 Stars
  • 853 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 3 % Grown

The README.md

FormHandlerBundle

Build Status, (*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
    }
}

Adding options to a form

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)

The Versions

11/05 2018

dev-master

9999999-dev

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

11/05 2018

1.0.0-rc3

1.0.0.0-RC3

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

11/05 2018

1.0.0-rc2

1.0.0.0-RC2

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

11/05 2018

dev-update-dependencies

dev-update-dependencies

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

13/06 2017

1.0.0-rc

1.0.0.0-RC

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

13/06 2017

dev-doctrine-entities

dev-doctrine-entities

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

25/05 2017

1.0.0-beta

1.0.0.0-beta

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

10/05 2017

1.0.0-alpha3

1.0.0.0-alpha3

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

02/04 2017

1.0.0-alpha2

1.0.0.0-alpha2

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms

23/03 2017

1.0.0-alpha1

1.0.0.0-alpha1

Easy form handling for Symfony forms

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

symfony symfony-bundle form handling symfony forms