w3r-one / json-schema-bundle

serialize Symfony Forms into JSON schema
MIT License
2 stars 0 forks source link

JsonSchemaBundle

A bundle to serialize a Symfony Form into a JSON Schema (RFC 2020-12).

Installation

$ composer require w3r-one/json-schema-bundle

If you're not using Symfony Flex, you've to register the bundle manually:

// config/bundles.php

return [
    // ...
    W3rOne\JsonSchemaBundle\W3rOneJsonSchemaBundle::class => ['all' => true],
];

Usage

namespace App\Controller;

use App\Entity\Partner;
use App\Form\PartnerType;
use W3rOne\JsonSchemaBundle\JsonSchema;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class FormController extends AbstractController
{
    public function partnerAdd(JsonSchema $jsonSchema): Response
    {
        $form = $this->createForm(PartnerType::class, new Partner(), ['validation_groups' => ['Default', 'Form-Partner']]);

        return new JsonResponse($jsonSchema($form));
    }
View the generated JSON Schema ```js { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "http://localhost/schemas/partner.json", "type": "object", "title": "partner", "properties": { "_token": { "type": "string", "title": "", "writeOnly": true, "default": "1996112795cc2bfa7d399fb.1rqGabut308UPJvtLSqXwgrrIXMqdei_M0T3DH53B50.tdzwLf-Atgdddf-qZF3dl127SABsENrMfiCdOAwRXvqBz_4Dz5SMfWMF6A", "options": { "widget": "hidden", "layout": "default" } }, "name": { "type": "string", "title": "Nom", "options": { "widget": "text", "layout": "default", } }, "types": { "type": "array", "title": "Types", "options": { "widget": "choice", "layout": "default", "attr": { "readonly": true }, "choice": { "expanded": true, "multiple": true, "filterable": true, "enumTitles": ["Client", "Fabricant", "Sous-traitant", "Installateur", "Fournisseur", "Concurrent", "Gestionnaire"] } }, "items": { "type": "string", "enum": ["customer", "manufacturer", "subcontractor", "installer", "supplier", "rival", "administrator"] } "uniqueItems": true }, "address": { "type": "object", "title": "Adresse", "options": { "widget": "address", "layout": "default" }, "properties": { "raw": { "type": "string", "title": "", "writeOnly": true, "options": { "widget": "text", "layout": "default", "attr": { "maxlength": 255, "placeholder": "Tapez une adresse" } } }, "formatted": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "coords": { "type": "object", "title": "", "options": { "widget": "coords", "layout": "default" }, "properties": { "lat": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "lng": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } } } }, "nb": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "street": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "zipcode": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "state": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "city": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } }, "country": { "type": "string", "title": "", "options": { "widget": "hidden", "layout": "default" } } } }, "url": { "type": "string", "title": "Site web", "options": { "widget": "url", "layout": "default" } }, "email": { "type": "string", "title": "Adresse email", "options": { "widget": "email", "layout": "default" } }, }, "required": [], "options": { "widget": "partner", "layout": "default", "form": { "method": "POST", "action": "http://localhost/partner/json_schema", "async": true } } } ```

Purpose

The goal behind this bundle is based on the fact that it is complicated for a modern front-end application to maintain a form component that is not mapped directly on a Symfony FormType.

Most of the time, the front-end component is defining form's props in a static way and if the back-end wants to update the form, we need to work twice, it's error prone and it's not extensible at all.

The main idea is to give the lead to the back-end, provide a JSON schema dynamically that will detail the full component and its related documentation ; the front-end "just" have to display and handle the form on his side.

If the Form is changing or even if it's dynamic based on some roles / scopes / etc., the front-end developer have nothing to change.

It's also allow working with forms directly in Twig and in the same time in a Javascript context.

The business rules are not duplicated and are only handled by the back-end.

This bundle doesn't provide any Front-End component, feel free to choose the stack that feet your needs to build your own Javascript Form.

Logic

Concrete exemple

This example allow to handle a form directly in Twig without XHR AND with async Javascript, feel free to drop completely the twig/not async part.

<?php

namespace App\Controller;

use App\Entity\Partner;
use App\Form\PartnerType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use W3rOne\JsonSchemaBundle\JsonSchema;
use W3rOne\JsonSchemaBundle\Utils;

class PartnerController extends AppAbstractController
{
    /**
     * @Entity("partner", expr="repository.findOne(partnerId)")
     */
    public function edit(Partner $partner, JsonSchema $jsonSchema, Request $request): Response
    {
        $form = $this->createForm(PartnerType::class, $partner, ['validation_groups' => ['Default', 'Form-Partner'], 'scope' => 'edit'])->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $this->em->flush();

                if ($request->isXmlHttpRequest()) {
                    return new JsonResponse([
                        'message' => 'The partner was successfully updated.',
                        'redirect_url' => $this->generateUrl('app_partner_show', ['partnerId' => $partner->getId()]),
                    ], Response::HTTP_OK);
                } else {
                    $this->addFlash('success', 'The partner was successfully updated.');

                    return $this->redirectToRoute('app_partner_show', ['partnerId' => $partner->getId()]);
                }
            } else {
                if ($request->isXmlHttpRequest()) {
                    return new JsonResponse([
                        'message' => 'There are errors in the form, please check.',
                        'errors' => Utils::getErrors($form),
                    ], Response::HTTP_BAD_REQUEST);
                } else {
                    $this->addFlash('error', 'There are errors in the form, please check.');
                }
            }
        }

        return $this->render('pages/partner/edit.html.twig', [
            'form' => $form->createView(),
            'partner' => $partner,
            'pageProps' => \json_encode([
                'form' => $jsonSchema($form),
                'errors' => Utils::getErrors($form),
                'partner' => \json_decode($this->apiSerializer->serialize($partner, ['default', 'partner'])),
            ]),
        ]);
    }
}

$this->em is a simple reference to the EntityManagerInterface.

$this->apiSerializer is a simple service based on the Symfony Serializer.

View the service ```php serializer = $serializer; } public function serialize($data, array $groups = ['default'], array $attributes = [], array $callbacks = []): string { $context = [ AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true, AbstractNormalizer::GROUPS => $groups, ]; if (!empty($attributes)) { $context[AbstractNormalizer::ATTRIBUTES] = $attributes; } if (!empty($callbacks)) { $context[AbstractNormalizer::CALLBACKS] = $callbacks; } return $this->serializer->serialize($data, 'json', $context); } } ```

Architecture

The resolver will traverse the form, guess the right Transformer for each property and apply recursive transformations based on the following schema.

Architecture

Legend

You can add your own transformers, or override/extend the transformers of your choice by yourself, see the dedicated section of this readme.

If needed, the form extension allow you to add custom props in w3r_one_json_schema to pass to your json specs.

Translator

This bundle relies on Symfony TranslatorInterface to translate:

The translation domain is dynamically retrieved from translation_domain option:

CSRF

If you've installed symfony/security-csrf and enabled crsf_protection on you FormType, the bundle will automatically add the correct csrf property (_token by default) with the default generated value (thanks to the TokenGeneratorInterface) in a hidden widget.

Built-in FormType

All Symfony FormTypes as 6.2 version are supported.

View the complete list * [TextType](https://symfony.com/doc/current/reference/forms/types/text.html) * [TextareaType](https://symfony.com/doc/current/reference/forms/types/textarea.html) * [EmailType](https://symfony.com/doc/current/reference/forms/types/eamil.html) * [PasswordType](https://symfony.com/doc/current/reference/forms/types/password.html) * [SearchType](https://symfony.com/doc/current/reference/forms/types/search.html) * [UrlType](https://symfony.com/doc/current/reference/forms/types/url.html) * [TelType](https://symfony.com/doc/current/reference/forms/types/tel.html) * [ColorType](https://symfony.com/doc/current/reference/forms/types/color.html) * [FileType](https://symfony.com/doc/current/reference/forms/types/file.html) * [RadioType](https://symfony.com/doc/current/reference/forms/types/radio.html) * [UuidType](https://symfony.com/doc/current/reference/forms/types/uuid.html) * [UlidType](https://symfony.com/doc/current/reference/forms/types/ulid.html) * [HiddenType](https://symfony.com/doc/current/reference/forms/types/hidden.html) * [IntegerType](https://symfony.com/doc/current/reference/forms/types/integer.html) * [MoneyType](https://symfony.com/doc/current/reference/forms/types/money.html) * [NumberType](https://symfony.com/doc/current/reference/forms/types/number.html) * [PercentType](https://symfony.com/doc/current/reference/forms/types/percent.html) * [RangeType](https://symfony.com/doc/current/reference/forms/types/range.html) * [ChoiceType](https://symfony.com/doc/current/reference/forms/types/choice.html) * [EnumType](https://symfony.com/doc/current/reference/forms/types/enum.html) * [EntityType](https://symfony.com/doc/current/reference/forms/types/entity.html) * [CountryType](https://symfony.com/doc/current/reference/forms/types/country.html) * [LanguageType](https://symfony.com/doc/current/reference/forms/types/language.html) * [LocaleType](https://symfony.com/doc/current/reference/forms/types/locale.html) * [TimezoneType](https://symfony.com/doc/current/reference/forms/types/timezone.html) * [CurrencyType](https://symfony.com/doc/current/reference/forms/types/currency.html) * [DateType](https://symfony.com/doc/current/reference/forms/types/date.html) * [DateTimeType](https://symfony.com/doc/current/reference/forms/types/datetime.html) * [TimeType](https://symfony.com/doc/current/reference/forms/types/time.html) * [WeekType](https://symfony.com/doc/current/reference/forms/types/week.html) * [BirthdayType](https://symfony.com/doc/current/reference/forms/types/birthday.html) * [DateIntervalType](https://symfony.com/doc/current/reference/forms/types/dateinterval.html) * [CollectionType](https://symfony.com/doc/current/reference/forms/types/collection.html) * [CheckboxType](https://symfony.com/doc/current/reference/forms/types/checkbox.html) * [ButtonType](https://symfony.com/doc/current/reference/forms/types/button.html) * [ResetType](https://symfony.com/doc/current/reference/forms/types/reset.html) * [SubmitType](https://symfony.com/doc/current/reference/forms/types/submit.html) * [RepeatedType](https://symfony.com/doc/current/reference/forms/types/repeated.html)

Supported JSON Schema specs

Unsupported JSON Schema specs (for now)

Additional JSON Schema specs

All non standards properties are wrapped into options property.

It includes:

If you want to pass other specific properties to your component, feel free to wrap them into w3r_one_json_schema property.

For example:

$builder
    ->add('name', TextType::class, [
        'label' => 'Name',
        'w3r_one_json_schema' => [
            'foo' => 'bar',
        ],
    ]);
{
    "name": {
        "type": "string",
        "title": "Name",
        "options": {
            "widget": "text",
            "layout": "default",
            "foo": "bar"
        }
    },
}

Override / Extend

You can totally override or extend any transformer / json specs of this bundle.

Widget / Layout resolving

In your FormTypes, you can override any widget / layout of your choice thanks to the w3r_one_json_schema option.

For example:

$builder
    ->add('address', TextType::class, [
        'label' => 'Address',
        'w3r_one_json_schema' => [
            'widget' => 'google_autocomplete',
            'layout' => 'two-cols',
        ],
    ]);
{
    "address": {
        "type": "string",
        "title": "address",
        "options": {
            "widget": "google_autocomplete",
            "layout": "two-cols",
        }
    },
}

You can also override the default layout globally if needed:

# config/packages/w3r_one_json_schema.yaml
w3r_one_json_schema:
    default_layout: 'fluid'

FormType Transformers

You can register your own transformers.

Tag them with the name w3r_one_json_schema.transformer and define the form_type you want to transform.

# config/services.yaml
services:
    App\JsonSchema\DateIntervalTypeTransformer:
        parent: W3rOne\JsonSchemaBundle\Transformer\AbstractTransformer
        tags:
            - { name: w3r_one_json_schema.transformer, form_type: 'date_interval'}

Your transformers are resolved before ours, so if you override an existing transformer, it'll be executed in place of the bundle built-in ones.

Transformers must implement the TransformerInterface.

The proper approach is to extend one of ours abstract or specific transformers, redefine method transform, call the parent function and extending/overwriting the json schema before returning it.

You can also implement directly the interface, but you've to manage everything by yourself in this case.

Example 1

You're using VichUploaderBundle and you want to serialize specific options of this bundle.

Just extend the ObjectTransformer, call the parent function, embed your json props and voila!

<?php

namespace App\JsonSchema;

use Symfony\Component\Form\FormInterface;
use W3rOne\JsonSchemaBundle\Transformer\ObjectTransformer;
use W3rOne\JsonSchemaBundle\Utils;

class VichFileTypeTransformer extends ObjectTransformer
{
    public function transform(FormInterface $form): array
    {
        $schema = parent::transform($form);
        $schema['options']['vichFile'] = [
            'allowDelete' => $form->getConfig()->getOption('allow_delete'),
            'downloadLink' => $form->getConfig()->getOption('download_link'),
            'downloadUri' => $form->getConfig()->getOption('download_uri'),
            'downloadLabel' => $this->translator->trans($form->getConfig()->getOption('download_label'), [], Utils::getTranslationDomain($form)),
            'deleteLabel' => $this->translator->trans($form->getConfig()->getOption('delete_label'), [], Utils::getTranslationDomain($form)),
        ];

        return $schema;
    }
}

Example 2

You want to add a PositionType as an integer.

Here we just extend the correct base IntegerTransformer.

<?php

namespace App\JsonSchema;

use W3rOne\JsonSchemaBundle\Transformer\IntegerTransformer;

class PositionTypeTransformer extends IntegerTransformer
{
}

Example 3

You want to override the TextareaType to replace it by a rich-text / wysiwyg editor.

<?php

namespace App\JsonSchema;

use Symfony\Component\Form\FormInterface;
use W3rOne\JsonSchemaBundle\Transformer\Type\TextareaTypeTransformer as BaseTextAreaTypeTransformer;

class TextareaTypeTransformer extends BaseTextAreaTypeTransformer
{
    public function transform(FormInterface $form): array
    {
        $schema = parent::transform($form);
        $schema['options']['widget'] = 'wysiwyg';
        $schema['options']['wysiwyg'] = [
            'config' => [
                // ...
            ],
        ];

        return $schema;
    }
}

Note that a better approach would have been to use a WysiwygType and to create a specific WysiwygTypeTransformer.