Git Product home page Git Product logo

formtojsonbundle's Introduction

FormToJsonBundle

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.

formtojsonbundle's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.