Coder Social home page Coder Social logo

form-handler-bundle's Introduction

The form handlers are designed to enhance the developer experience (DX) when working with Symfony forms. It makes the controllers simpler by moving the form success and failure flows to a separate class.

class YourController extends Controller
{
    public function formAction(Request $request, MyEntityUser $user)
    {
        $handler = $this->get('hostnet.form_handler.factory')->create(MyFormHandler::class);

        if (($response = $handler->handle($request, new MyFormData())) instanceof RedirectResponse) {
            return $response;
        }

        // regular or in-valid flow
        return $this->render->renderView('/your/form.html.twig', [
            'form' => $handler->getForm()->createView()
        ]);
    }
}

By extracting the success - and if available, the failure - flows, you reduce the amount of code in your controllers, which in turn, achieves slim controllers. The definition of a controller is according to Symfony: "a PHP function you create that reads information from the Symfony's Request object and creates and returns a Response object".

Installation

Documentation

License

The hostnet/form-handler-bundle is licensed under the MIT License, meaning you can reuse the code within proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice.

Get in touch

form-handler-bundle's People

Contributors

gwendolenlynch avatar janlam7 avatar jaspers2307 avatar linaori avatar mvannes avatar mvrhov avatar nicoschoenmaker avatar nutama avatar ping-localhost avatar scrutinizer-auto-fixer avatar sstok avatar svandervlugt avatar wouterj avatar xabbuh avatar yannickl88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

form-handler-bundle's Issues

Handlers stop sharing instances by using `shared: false`

The handlers are stateful, that's because it contains setters and the data object is integrated inside. There's no real way to prevent this without losing information about types. While the form handlers are services, you never use them like this as they get passed along with the action arguments.

Due to their stateful nature, I'd like to have the collector compiler pass add the shared option as false to all handlers during container compilation. It might be a slight behavioural change, but I consider this a bug because it gives unexpected results if the handlers are shared.

Would there be any implications when adding this option?

Fix deprecations in latest symfony

The "app.handler.legacy.named" service is private, getting it from the container is 
deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service 
public, or stop using the container directly and use dependency injection instead
Using the KERNEL_DIR environment variable or the automatic guessing based on 
the phpunit.xml / phpunit.xml.dist file location is deprecated since 3.4. Set the 
KERNEL_CLASS environment variable to the fully-qualified class name of your 
Kernel instead. Not setting the KERNEL_CLASS environment variable will throw 
an exception on 4.0 unless you override the :createKernel() or :getKernelClass() 
method

See https://travis-ci.org/hostnet/form-handler-bundle/jobs/264685602

Adding custom errors to form in onSuccess-case

Is it possible to get the current Form-instance when I handle the post-processing in onSuccess?

The reason I need the form is that I update a present entity using a DTO (which is defined as the data_class-attribute of the form) and trigger the UniqueEntity-validator.

Add autoconfigure support

Currently I have to add some config in all my applications where I define form handlers:

services:
    _instanceof:
        Hostnet\Component\Form\FormHandlerInterface:
            tags: [{ name: form.handler }]
            public: true
        Hostnet\Component\FormHandler\HandlerTypeInterface:
            tags: [{ name: form.handler }]
            public: true

See if we can use the simple-phpunit script for tests

As @xabbuh mentioned in #30 (comment), listeners and such would be automatically registered using the simple-phpunit script provided by the symfony/phpunit-bridge package that we use. Right now we are depending on this in our dev dependencies "phpunit/phpunit": "^4.7|^5.0",. I think it's a viable alternative to check if we can use the bridge for this.

Manual form submit

Hi guys,

I have a POST API endpoint. When I send request to this URL using POSTMAN

$handler->handle($request, $cardData) function returns null as a result of form not being submitted. Without this bundle I used to manually submit data to the form using this example https://symfony.com/doc/current/form/direct_submit.html. Is there any workout how to manually submit data to the handler?

Using the `ArgumentValueResolver` instead of `ParameterConverter`

Currently we use the [ParameterConverter] to provide the handlers to the action, but this causes 2 side effects:

  • Mandatory dependency on the SensioFrameworkExtraBundle, depending on bundles is usually not the best idea.
  • The arguments are stored in the Request->attributes, this means that your request contains services which is not really nice.

I would like to replace the converter by an ArgumentValueResolver. It would introduce a BC break, so perhaps we can let developers toggle for BC reasons. After updating, a parameter could be set to revert to the old behavior in case the symfony version is not 3.1. This way we could support both variants on demand and automatically until the majority of people starts using ^3.1.

Perhaps we could still support the parameter converter up until 3.4 is released and remove it afterwards, while making the composer dependency a suggests earlier.

Clean up functional tests

Currently the functional tests are a bit messy and contain a lot of duplication. Suggestion is to omit the phpunit.xml.dist constant for the kernel and simply use the getKernelClass. Running the tests at the moment, requires you to remove the var dir after certain changes, which is quite cumbersome. Additionally, we can use options to pass along which config file to load.

How to use?

Some usage examples in the README would be really helpful! I sorta get how to use this, but at the same time I don't.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.