定制 ansien/form-to-json-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ansien/form-to-json-bundle

Composer 安装命令:

composer require ansien/form-to-json-bundle

包简介

Convert Symfony forms to JSON

README 文档

README

Latest Version on Packagist Total Downloads GitHub

This bundle will allow you to transform Symfony forms into JSON.

Installation

You can install the package via Composer:

composer require ansien/form-to-json-bundle

Usage

Controller:

<?php

declare(strict_types=1);

namespace App\Controller;

use Ansien\FormToJsonBundle\Transformer\Service\FormTransformerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Example;
use App\Form\ExampleType;

class ExampleController extends AbstractController
{
    public function __construct(private FormTransformerInterface $formTransformer) 
    {
    }

    #[Route('/example', methods: ['GET'])]
    public function __invoke(Request $request): JsonResponse
    {
        $example = new Example('Hello!');
        $form = $this->createForm(ExampleType::class, $example);
        
        return new JsonResponse($this->formTransformer->transform($form));
    }
}

Example output:

{
  "schema": {
    "id": "test",
    "name": "test",
    "type": "super_form",
    "disabled": false,
    "label": null,
    "label_format": null,
    "label_html": false,
    "multipart": true,
    "unique_block_prefix": "_test",
    "row_attr": [],
    "translation_domain": null,
    "label_translation_parameters": [],
    "attr_translation_parameters": [],
    "valid": true,
    "required": true,
    "size": null,
    "label_attr": [],
    "help": null,
    "help_attr": [],
    "help_html": false,
    "help_translation_parameters": [],
    "compound": true,
    "method": "POST",
    "action": "",
    "submitted": false,
    "attr": [],
    "children": {
      "text": {
        "id": "test_text",
        "name": "text",
        "type": "text",
        "disabled": false,
        "label": null,
        "label_format": null,
        "label_html": false,
        "multipart": false,
        "unique_block_prefix": "_test_text",
        "row_attr": [],
        "translation_domain": null,
        "label_translation_parameters": [],
        "attr_translation_parameters": [],
        "valid": true,
        "required": true,
        "size": null,
        "label_attr": [],
        "help": null,
        "help_attr": [],
        "help_html": false,
        "help_translation_parameters": [],
        "compound": false,
        "method": "POST",
        "action": "",
        "submitted": false,
        "attr": []
      }
    }
  },
  "values": {
    "text": "Test 123"
  },
  "errors": {
    "_global": [
      "Test root error"
    ],
    "text": [
      "Test error"
    ]
  }
}

Extending the bundle

You can create your own form type transformer by making a new service that extends AbstractTypeTransformer and tagging it with form_to_json_bundle.type_transformer.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Supporters

Stargazers repo roster for @ansien/FormToJsonBundle

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-03-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固