Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Easy form handling for Symfony forms

License

NotificationsYou must be signed in to change notification settings

SolidWorx/FormHandlerBundle

Repository files navigation

Build Status

The FormHandler component attempts to make controllers with basic form handlers cleaner by off-loading form handling to separate classes.

Table of Contents

Requirements

FormHandler requires PHP 7.1+ and Symfony 3.0+

Installation

Composer

$ 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(),        ];// ...    )

Usage

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    }}

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 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');    }}

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.

Testing

To run the unit tests, execute the following command

$ vendor/bin/phpunit

Contributing

SeeCONTRIBUTING

License

FormHandler is open-sourced software licensed under theMIT license

Please see theLICENSE file for the full license.


[8]ページ先頭

©2009-2025 Movatter.jp