wemxo/dynamic-form-bundle
Composer 安装命令:
composer require wemxo/dynamic-form-bundle
包简介
Useful symfony bundle that offers the possibility to create dynamic form types based on a given configuration.
README 文档
README
Dynamic form bundle gives you the ability to create forms dynamically based on a given configuration.
Requirements / Dependencies
- PHP 7.4 or higher
- symfony/framework-bundle 5.0 or higher
- symfony/form 5.0 or higher
- symfony/finder 5.0 or higher
Usage
Installation
Install the latest version
composer require wemxo/dynamic-form-bundle
Activation
Enable the bundle in config/bundle.php
<?php return [ /* ... */ Wemxo\DynamicFormBundle\DynamicFormBundle::class => ['all' => true], /* ... */ ];
Configuration
Add bundle configuration
# config/packages/dynamic_form.yaml dynamic_form: recursive: true # Browse config folders recursively config_paths: # Files configuration paths - '%kernel.project_dir%/config/dynamic_form' when@prod: framework: cache: pools: dynamic_form_pool_cache: adapter: cache.adapter.redis dynamic_form: cache_pool: dynamic_form_pool_cache # Use cache 'dynamic_form_pool_cache' to store parsed configuration (recommended in production)
Example of form configuration
YAML format
# config/dynamic_form/address.yaml france: form: subscribers: ~ transformers: ~ fields: address: &address transformers: ~ type: &textType Symfony\Component\Form\Extension\Core\Type\TextType options: label: Address required: true attr: placeholder: Enter your address constraints: - class: ¬Blank Symfony\Component\Validator\Constraints\NotBlank options: message: You must enter your address addressComplement: &addressComplement type: *textType options: label: Address complement required: false attr: placeholder: Enter your address complement constraints: ~ postalCode: &postalCode type: *textType options: label: Postal code required: true attr: placeholder: Enter your postal code constraints: - class: *notBlank options: message: You must enter your postal code italy: form: subscribers: ~ transformers: ~ fields: address: *address addressComplement: *addressComplement postalCode: *postalCode department: &department type: *textType options: label: Department required: true attr: placeholder: Enter your department constraints: - class: *notBlank options: message: You must enter your department morocco: form: subscribers: ~ transformers: ~ fields: address: *address postalCode: *postalCode city: type: *textType options: label: City required: true attr: placeholder: Enter your city constraints: - class: *notBlank options: message: You must enter your city
PHP format
<?php return [ 'france' => [ 'form' => [ 'subscribers' => NULL, 'transformers' => NULL, 'fields' => [ 'address' => [ 'transformers' => NULL, 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Address', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your address', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your address', ], ], ], ], 'addressComplement' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Address complement', 'required' => false, 'attr' => [ 'placeholder' => 'Enter your address complement', ], ], 'constraints' => NULL, ], 'postalCode' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Postal code', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your postal code', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your postal code', ], ], ], ], ], ], ], 'italy' => [ 'form' => [ 'subscribers' => NULL, 'transformers' => NULL, 'fields' => [ 'address' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Address', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your address', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your address', ], ], ], ], 'addressComplement' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Address complement', 'required' => false, 'attr' => [ 'placeholder' => 'Enter your address complement', ], ], 'constraints' => NULL, ], 'postalCode' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Postal code', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your postal code', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your postal code', ], ], ], ], 'department' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Department', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your department', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your department', ], ], ], ], ], ], ], 'morocco' => [ 'form' => [ 'subscribers' => NULL, 'transformers' => NULL, 'fields' => [ 'address' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Address', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your address', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your address', ], ], ], ], 'postalCode' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'Postal code', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your postal code', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your postal code', ], ], ], ], 'city' => [ 'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', 'options' => [ 'label' => 'City', 'required' => true, 'attr' => [ 'placeholder' => 'Enter your city', ], ], 'constraints' => [ 0 => [ 'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank', 'options' => [ 'message' => 'You must enter your city', ], ], ], ], ], ], ], ];
Create form
<?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Wemxo\DynamicFormBundle\Form\DynamicType; use Wemxo\DynamicFormBundle\Utils\Helper\DynamicFormHelper; class AppController extends AbstractController { #[Route(name: 'app_home')] public function home(FormFactoryInterface $formFactory): Response { $user = $this->getUser(); $countryLabel = $user->getCountry()->getLabel(); $form = $formFactory->create(DynamicType::class, null, [ 'dynamic_key' => DynamicFormHelper::configKey('address', $countryLabel), ]); return $this->render('app/home.html.twig', [ 'form' => $form->createView() ]); } }
Create form with block prefix
<?php declare(strict_types=1); namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Wemxo\DynamicFormBundle\Form\DynamicType; use Wemxo\DynamicFormBundle\Utils\Helper\DynamicFormHelper; class AppController extends AbstractController { #[Route(name: 'app_home')] public function home(FormFactoryInterface $formFactory): Response { $user = $this->getUser(); $countryLabel = $user->getCountry()->getLabel(); $form = $formFactory->createNamed('my_custom_prefix', DynamicType::class, null, [ 'dynamic_key' => DynamicFormHelper::configKey('address', $countryLabel), ]); return $this->render('app/home.html.twig', [ 'form' => $form->createView() ]); } }
Use form event subscriber
1 - Create a class by implementing the interface Wemxo\DynamicFormBundle\EventSubscriber\DynamicFormEventSubscriber.
1.1 - Implement getName function.
1.2 - Implement getSubscribedEvents function.
<?php declare(strict_types=1); namespace App\Form; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Wemxo\DynamicFormBundle\EventSubscriber\DynamicFormEventSubscriber; class AddressFormEventSubscriber implements DynamicFormEventSubscriber { public function getName(): string { return 'address_event_subscriber'; } public static function getSubscribedEvents(): array { return [ FormEvents::PRE_SET_DATA => 'onPreSetData', ]; } public function onPreSetData(FormEvent $event): void { // do something. } }
1.3 - Update form configuration
france: form: subscribers: - address_event_subscriber # The value returned from the getName function. fields: []
Use form model transformer
1 - Create a class by implementing the interface Wemxo\DynamicFormBundle\DataTransformer\DynamicDataTransformer.
1.1 - Implement getName function.
1.2 - Implement transform function.
1.3 - Implement reverseTransform function.
<?php declare(strict_types=1); namespace App\Form; use Wemxo\DynamicFormBundle\DataTransformer\DynamicDataTransformer; class AddressFormDataTransformer implements DynamicDataTransformer { public function getName(): string { return 'address_data_transformer'; } public function transform(mixed $value): mixed { if (is_array($value)) { return json_encode($value, JSON_PRETTY_PRINT); } return $value; } public function reverseTransform(mixed $value): mixed { return $value; } }
1.3 - Update form configuration
france: form: transformers: - address_data_transformer # The value returned from the getName function. fields: []
wemxo/dynamic-form-bundle 适用场景与选型建议
wemxo/dynamic-form-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 357 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「form」 「bundle」 「dynamic」 「wemxo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wemxo/dynamic-form-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wemxo/dynamic-form-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wemxo/dynamic-form-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Flexslider slideshow content block for Silverstripe Elemental
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
EAV modeling package for Eloquent and Laravel.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provide a way to secure accesses to all routes of an symfony application.
统计信息
- 总下载量: 357
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-28