codete/form-generator-bundle 问题修复 & 功能扩展

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

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

codete/form-generator-bundle

最新稳定版本:2.0.0

Composer 安装命令:

composer require codete/form-generator-bundle

包简介

Symfony Bundle for dynamic Form generation

README 文档

README

Build Status SensioLabsInsight

We were extremely bored with writing/generating/keeping-up-to-date our FormType classes so we wanted to automate the process and limit required changes only to Entity/Document/Whatever class and get new form out of the box - this is how FormGenerator was invented.

You're looking at the documentation for version 2.0

Basic Usages

Consider a class

use Codete\FormGeneratorBundle\Annotations as Form;
// import Symfony form types so ::class will work

/**
 * @Form\Form(
 *  personal = { "title", "name", "surname", "photo", "active" },
 *  work = { "salary" },
 *  admin = { "id" = { "type" = NumberType::class }, "surname" }
 * )
 */
class Person
{
    public $id;
    
    /**
     * @Form\Field(type=ChoiceType::class, choices = { "Mr." = "mr", "Ms." = "ms" })
     */
    public $title;
    
    /**
     * @Form\Field(type=TextType::class)
     */
    public $name;
    
    /**
     * @Form\Field(type=TextType::class)
     */
    public $surname;
    
    /**
     * @Form\Field(type=FileType::class)
     */
    public $photo;
    
    /**
     * @Form\Field(type=CheckboxType::class)
     */
    public $active;
    
    /**
     * @Form\Field(type=MoneyType::class)
     */
    public $salary;
}

Now instead of writing whole PersonFormType and populating FormBuilder there we can use instead:

use Codete\FormGeneratorBundle\FormGenerator;

$generator = $this->get(FormGenerator::class);

$person = new Person();
$form = $generator->createFormBuilder($person)->getForm();
$form->handleRequest($request);

Voila! Form for editing all annotated properties is generated for us. We could even omit type=".." in annotations if Symfony will be able to guess the field's type for us.

Specifying Field Options

By default everything you specify in @Form\Field (except for type) annotation will be passed as an option to generated form type. To illustrate:

/**
 * @Form\Field(type=ChoiceType::class, choices = { "Mr." = "mr", "Ms." = "ms" }, "attr" = { "class" = "foo" })
 */
public $title;

is equivalent to:

$fb->add('title', ChoiceType::class, [
    'choices' => [ 'Mr.' => 'mr', 'Ms.' => 'ms' ],
    'attr' => [ 'class' => 'foo' ],
]);

This approach has few advantages like saving you a bunch of keystrokes each time you are specifying options, but there are downsides too. First, if you have any custom option for one of your modifiers you forget to unset, Symfony will be unhappy and will let you know by throwing an exception. Another downside is that we have reserved type property and it's needed as an option for the repeated type. If you ever find yourself in one of described cases, or you just prefer to be explicit, you can put all Symfony fields' options into an options property:

/**
 * @Form\Field(
 *   type=ChoiceType::class,
 *   options={ "choices" = { "Mr." = "mr", "Ms." = "ms" }, "attr" = { "class" = "foo" } }
 * )
 */
public $title;

When Form Generator creates a form field and finds options property, it will pass them as that field's options to the FormBuilder. Effectively this allows you to separate field's options from options for your configuration modifiers which can be a gain on its own.

Adding fields not mapped to a property

Sometimes you may need to add a field that will not be mapped to a property. An example of such use case is adding buttons to the form:

/**
 * The first value in Field annotation specifies field's name.
 *
 * @Form\Field("reset", type=ResetType::class)
 * @Form\Field("submit", type=SubmitType::class, "label"="Save")
 */
class Person

All fields added on the class level come last in the generated form, unless a form view (described below) specifies otherwise. Contrary to other class-level settings, @Fields will not be inherited by child classes.

Form Views

In the example we have defined additional form views in @Form\Form annotation so we can add another argument to createFormBuilder

$form = $generator->createFormBuilder($person, 'personal')->getForm();

And we will get Form with properties specified in annotation. We can also add/override fields and their properties like this:

/**
 * @Form\Form(
 *  work = { "salary" = { "attr" = { "class" = "foo" } } }
 * )
 */
class Person

But if you need something more sophisticated than Annotations we have prepared few possibilities that can be either added manually or by tagging your services. For each of them FormGenerator allows you to pass any additional informations you want in optional $context argument. Both ways allows you to specify priority which defines order of execution (default is 0, if two or more services have same priority then first added is executed first).

If you have enabled Service autoconfiguration the bundle will automatically tag services for you.

FormViewProvider

These are used to provide fields list and/or basic configuration for Forms and are doing exactly same thing as @Form\Form annotation.

Tag for service: form_generator.view_provider

FormConfigurationModifier

These can modify any form configuration provided by class itself or FormViewProviders. Feel free to remove or add more stuff to your Form or tweak existing configuration

Tag for service: form_generator.configuration_modifier

class InactivePersonModifier implements FormConfigurationModifierInterface
{
    public function modify($model, $configuration, $context) 
    {
        unset($configuration['salary']);
        return $configuration;
    }

    public function supports($model, $configuration, $context) 
    {
        return $model instanceof Person && $model->active === false;
    }
}

FormFieldResolver

These are responsible for creating actual field in Form and can be used for instance to attach Transformers to your fields.

Tag for service: form_generator.field_resolver

class PersonSalaryResolver implements FormFieldResolverInterface
{
    public function getFormField(FormBuilderInterface $fb, $field, $type, $options, $context) 
    {
        $transformer = new /* ... */;
        return $fb->create($field, $type, $options)
                ->addViewTransformer($transformer);
    }

    public function supports($model, $field, $type, $options, $context) 
    {
        return $model instanceof Person && $field === 'salary';
    }
}

Embedded Forms

If you need embedded forms we got you covered:

/**
 * @Form\Embed(class="Codete\FormGeneratorBundle\Tests\Model\Person")
 */
public $person;

Such sub-form will contain all annotated properties from given model. To specify a view for the generated embedded form just specify it in the configuration:

/**
 * @Form\Embed(
 *  class="Codete\FormGeneratorBundle\Tests\Model\Person",
 *  view="work"
 * )
 */
public $employee;

codete/form-generator-bundle 适用场景与选型建议

codete/form-generator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.99k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2015 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form generator」 「dynamic form」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codete/form-generator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codete/form-generator-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 codete/form-generator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 7
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-09