PHPackages                             precision-soft/symfony-json-form - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. precision-soft/symfony-json-form

ActiveSymfony-bundle[Parsing &amp; Serialization](/categories/parsing)

precision-soft/symfony-json-form
================================

forms that serialize to json for frontend frameworks with symfony api backends

v1.0.7(2w ago)020MITTypeScriptPHP &gt;=8.2

Since Sep 18Pushed 2mo agoCompare

[ Source](https://github.com/precision-soft/symfony-json-form)[ Packagist](https://packagist.org/packages/precision-soft/symfony-json-form)[ Docs](https://github.com/precision-soft/symfony-json-form)[ RSS](/packages/precision-soft-symfony-json-form/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (8)Dependencies (32)Versions (9)Used By (0)

Symfony json form
=================

[](#symfony-json-form)

**You may fork and modify it as you wish**.

Any suggestions are welcomed.

Purpose
-------

[](#purpose)

The purpose of this library is to create forms for single page applications, with a symfony backend. The forms are constructed in the backend and serialized to json, that can be rendered in the frontend. In the assets folder you can find a react component to render the form.

A form is described by three pieces that you provide per form:

- a **DTO** (`DtoInterface`) — the typed data structure the form maps to and from;
- a **form service** (`AbstractFormService`) — declares the HTTP method, the submit action, and the elements;
- the **elements** — the individual fields (`NumberElement`, `StringElement`, ...).

`render()` serializes the form (plus the DTO values) to a json structure for the frontend; `handleRequest()` takes the incoming request, sanitizes it, and denormalizes it back into the DTO.

### V1 vs V2

[](#v1-vs-v2)

There are 2 versions of the react renderer. They consume the **same** backend json — only the frontend components differ:

- [formV1](./assets/react/formV1) — the original react components, kept for backwards compatibility.
- [formV2](./assets/react/formV2) — the new components and the **recommended** way to render the json.

Usage
-----

[](#usage)

Add this to your **services.yaml** so every form service receives the serializer:

```
services:
    _instanceof:
        PrecisionSoft\Symfony\JsonForm\Service\Contract\AbstractFormService:
            calls:
                - [ setSerializer, [ '@serializer' ] ]
```

A form service must implement four abstract methods: `getDtoClass()`, `getMethod()`, `getAction(DtoInterface $dto)`and `build(Form $form, DtoInterface $dto)`.

```
