micayael / form-generator-bundle
使用yaml和json生成表单的Bundle
1.0.9
2023-06-28 19:05 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- symfony/config: ^5.1|^6.0
- symfony/dependency-injection: ^5.1|^6.0
- symfony/form: ^5.1|^6.0
- symfony/http-kernel: ^5.1|^6.0
- symfony/validator: ^5.1|^6.0
- symfony/yaml: ^5.1|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0|^3.1
- phpmd/phpmd: @stable
- phpstan/phpstan: ^1.4
- symfony/framework-bundle: ^5.1|^6.0
- symfony/test-pack: ^1.0
- symfony/var-dumper: ^5.1|^6.0
README
介绍
允许您使用框架表单组件的标准配置,通过YAML、JSON或关联数组配置来生成Symfony表单。
参见: https://symfony.com.cn/doc/current/reference/forms/types.html
它基于使用表单构建器的add函数,该函数用于创建使用Symfony的表单
- 表单输入元素的名称
- 表单字段的类型
- 一个选项数组来配置此字段类型
// FormInterface::add($child, string $type = null, array $options = []) $builder->add($inputName, $inputTypeClass, $inputOptions);
通过这个,可以创建一个YAML配置、JSON或关联数组,其中
- 键表示
- 类型表示由Bundle支持的表单
- 或类表单类型的“完全限定类名(FQN”
- 选项数组表示表单
这允许您使用yaml、json或关联数组等配置动态表单,如下面的示例所示
YAML
name: birthday: type: date options: label: 'Your Birthday' status: type: choice options: choices: Active: A Inactive: I custom_type: type: App\Form\Type\CustomType options: custom_option: value
JSON
{ "name": null, "birthday": { "type": "date", "options": { "label": "Your Birthday" } }, "status": { "type": "choice", "options": { "choices": { "Active": "A", "Inactive": "I" } } }, "custom_type": { "type": "App\\Form\\Type\\CustomType", "options": { "custom_option": "value" } } }
PHP
$formConfig = [ 'name' => NULL, 'birthday' => [ 'type' => 'date', 'options' => [ 'label' => 'Your Birthday', ], ], 'status' => [ 'type' => 'choice', 'options' => [ 'choices' => [ 'Active' => 'A', 'Inactive' => 'I', ], ], ], 'custom_type' => [ 'type' => 'App\\Form\\Type\\CustomType', 'options' => [ 'custom_option' => 'value', ], ], ]
安装
composer require micayael/form-generator-bundle
用法
在需要创建表单的地方,例如控制器或服务中,您必须注入由Bundle提供的FormGenerator对象。
class HomeController extends AbstractController { /** @required */ public FormGenerator $formGenerator; public function __invoke(Request $request): Response { $formConfigArray = []; // configuration as associative array // Gets a FormInterface object with the configured form $form = $this->formGenerator->createForm($formConfigArray); $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()){ $formData = $form->getData(); // process your form } }
FormGenerator服务提供了以下方法
- FormGenerator::createForm(array $formConfig, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从关联数组创建表单
- FormGenerator::createFormFromJson(string $formConfigJson, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从JSON字符串创建表单
- FormGenerator::createFormFromYaml(string $formConfigYaml, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从YAML字符串创建表单
方法参数
- array $formConfig: 表单配置
- array $formOptions = []: 自定义表单选项
- $data = null: 表单数据
- string $baseFormTypeClass = null: 将用作添加其他字段的基类的表单类(FQN)。如果未传递,它们将添加到空表单中。
- string $groupName = null: 将用于将由
参数创建的字段作为嵌入表单的组名
开发
安装依赖项
composer install
测试
vendor/bin/phpunit
代码审查
- phpstan级别: https://phpstan.org/user-guide/rule-levels
vendor/bin/phpstan analyse src tests --level 5
vendor/bin/phpmd ./ text .phpmd-ruleset.xml --exclude var,vendor