PHP code example of ansien / form-to-json-bundle

1. Go to this page and download the library: Download ansien/form-to-json-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ansien / form-to-json-bundle example snippets




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));
    }
}