mitom/form-handler-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mitom/form-handler-bundle

最新稳定版本:0.2

Composer 安装命令:

composer require mitom/form-handler-bundle

包简介

Form handler bundle for symfony.

README 文档

README

The aim of this bundle is to simplify the management and submission of forms and provide a level of abstraction over their submission. You will possibly gain the following advantages by using it:

  • Fewer dependencies in Controllers
  • Easily testable form handling
  • Extracting business logic from Controllers
  • More re-usable code

It is in an early stage and changes may occur, however the interfaces are unlikely to change. It is recommended that you follow versions, instead of branches (as in ~0.1).

Installation

Add it in your composer.json:

{
    "require" : {
        "mitom/form-handler-bundle" : "~0.1"
    }
}

Then add the bundle in your AppKernel.php:

    $bundles = [
        // ...
        new Mitom\Bundle\FormHandlerBundle\FormHandlerBundle()
    ];

Usage

First of you will need a FormType to work with, you can create one according to the official symfony documentation. For example:

namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('task')
            ->add('dueDate', null, ['widget' => 'single_text'])
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'task';
    }
}

Next you will have to create your FormHandler for it. An AbstractFormHandler is provided in the bundle to make it easier for you, but of course you don't have to use it. The FormHandler only has to implement the FormHandlerInterface.

namespace Acme\TaskBundle\Form\Handler;

use Mitom\Bundle\FormHandlerBundle\Handler\AbstractFormHandler;
use Acme\Entity\Task;
use Acme\Entity\Form\Type\TaskType;
use Symfony\Component\Routing\RouterInterface;

class TaskFormHandler extends AbstractFormHandler
{
    private $router;

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

    /** @inheritDoc */
    public function getType()
    {
        /**
         * In case your FormType is a service, you could just return
         * its' alias here as a string and let the FormFactory create it
         * for you.
         */
        return new TaskType();
    }

    public function onSuccess(FormData $formData)
    {
        /**
         * do whatever you want, like persisting to database
         */

        return new RedirectResponse($this->router->generate('acme.task', ['task' => $formData->getData()->getId()]));
    }

    public function onError(FormData $formData)
    {
        /**
         * do whatever you want, like log the error or simply return the FormData.
         */

        return ['form' => $formData->getForm()->createView()];
    }
}

The next step is to register the FormHandler as a service and tagging it with mitom.form_handler:

acme_task.task.form_handler:
    class: Acme\TaskBundle\Form\Handler\TaskFormHandler
    arguments:
        - "@router"
    tags:
        - { name: "mitom.form_handler" }

In your controller inject the mitom_form_handler.manager service:

acme_task.task.controller:
    class: Acme\TaskBundle\Controller\TaskController
    arguments:
        - "@mitom_form_handler.manager"

Alternatively you could inject a FormHandler straight away, if you only need a single handler in your controller. However I'd recommend going through the Manager anyway, as it makes it easy to user other handlers later and keeps thigs consistent.

And finally make use of it:

namespace Acme\TaskBundle\Controller;

use Mitom\Bundle\FormHandlerBundle\FormData;
use Mitom\Bundle\FormHandlerBundle\FormHandlerManager;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TaskController
{
    protected $formHandlerManager;

    /**
     * @param FormHandlerManager $formHandlerManager
     */
    public function __construct(FormHandlerManager $formHandlerManager)
    {
        $this->formHandlerManager = $formHandlerManager;
    }


    /**
     * @Template()
     */
    public function newAction()
    {
        return ['form' => $this->formHandlerManager()->getHandler('task')->createForm()->createView()];
    }


    /**
     * @Template()
     */
    public function createAction(Request $request)
    {
        $formData = new FormData();
        $formData->setRequest($request);

        // note that you can get the handler by using the name of the FormType
        return $this->formHandlerManager()->getHandler('task')->handle($formData);
    }
}

The example above is using the Template annotation to make it shorter, it is however not a dependency of this bundle.

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL
  • 更新时间: 2014-11-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固