Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Form To Json Bundle Laravel Package

ansien/form-to-json-bundle

View on GitHub
Deep Wiki
Context7

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope