micayael/form-generator-bundle

使用yaml和json生成表单的Bundle

安装: 3 873

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.9 2023-06-28 19:05 UTC

This package is auto-updated.

Last update: 2024-08-30 01:08:27 UTC


README

Symfony 5 Symfony 6 Scrutinizer Quality Score Packagist License Latest Stable Version Total Downloads PHP from Packagist

介绍

允许您使用框架表单组件的标准配置,通过YAML、JSON或关联数组配置来生成Symfony表单。

参见: https://symfony.com.cn/doc/current/reference/forms/types.html

它基于使用表单构建器的add函数,该函数用于创建使用Symfony的表单

  1. 表单输入元素的名称
  2. 表单字段的类型
  3. 一个选项数组来配置此字段类型
// 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服务提供了以下方法

  1. FormGenerator::createForm(array $formConfig, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从关联数组创建表单
  2. FormGenerator::createFormFromJson(string $formConfigJson, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从JSON字符串创建表单
  3. FormGenerator::createFormFromYaml(string $formConfigYaml, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): 从YAML字符串创建表单

方法参数

  1. array $formConfig: 表单配置
  2. array $formOptions = []: 自定义表单选项
  3. $data = null: 表单数据
  4. string $baseFormTypeClass = null: 将用作添加其他字段的基类的表单类(FQN)。如果未传递,它们将添加到空表单中。
  5. string $groupName = null: 将用于将由参数创建的字段作为嵌入表单的组名

开发

安装依赖项

composer install

测试

vendor/bin/phpunit

代码审查

vendor/bin/phpstan analyse src tests --level 5
vendor/bin/phpmd ./ text .phpmd-ruleset.xml --exclude var,vendor