PHPackages                             starx/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. starx/symfony-json-form

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

starx/symfony-json-form
=======================

Library to transform Symfony Forms into Json Schema

v0.17.0(5y ago)09MITPHPPHP ^7.2.5

Since Jan 31Pushed 1mo agoCompare

[ Source](https://github.com/starx/Symfony-Json-Form)[ Packagist](https://packagist.org/packages/starx/symfony-json-form)[ RSS](/packages/starx-symfony-json-form/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (9)Versions (18)Used By (0)

Liform
======

[](#liform)

Liform is a library for serializing Symfony Forms into [JSON schema](http://json-schema.org/). It can be used along with [liform-react](https://github.com/Limenius/liform-react) or [json-editor](https://github.com/jdorn/json-editor), or any other form generator based on json-schema.

It is used by [LiformBundle](https://github.com/Limenius/LiformBundle) but can also be used as a stand-alone library.

It is very annoying to maintain backend forms that match forms in a client technology, such as JavaScript. It is also annoying to maintain a documentation of such forms. And error prone.

Liform generates a JSON schema representation, that serves as documentation and can be used to document, validate your data and, if you want, to generate forms using a generator.

[![Build Status](https://camo.githubusercontent.com/3b7670c7e04839e4aaf2a2dc217b74ea1eccff1ab685fd443c4e892374f5e580/68747470733a2f2f7472617669732d63692e6f72672f4c696d656e6975732f4c69666f726d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Limenius/Liform)[![Latest Stable Version](https://camo.githubusercontent.com/add2f21507696969eeae03b75b22302352b236c50281f186457a43076abfc867/68747470733a2f2f706f7365722e707567782e6f72672f6c696d656e6975732f6c69666f726d2f762f737461626c65)](https://packagist.org/packages/limenius/liform)[![Latest Unstable Version](https://camo.githubusercontent.com/1b4ecad56e55341a28cc7b1b945257b4476131f3dc2147a657d7e020ce0df651/68747470733a2f2f706f7365722e707567782e6f72672f6c696d656e6975732f6c69666f726d2f762f756e737461626c65)](https://packagist.org/packages/limenius/liform)[![License](https://camo.githubusercontent.com/2f758e69880f7d20b98c82571c5d17478ea8a38d54923e8d1202130898d9d463/68747470733a2f2f706f7365722e707567782e6f72672f6c696d656e6975732f6c69666f726d2f6c6963656e7365)](https://packagist.org/packages/limenius/liform)

Installation
------------

[](#installation)

Open a console, enter your project directory and execute the following command to download the latest stable version of this library:

```
$ composer require limenius/liform

```

This command requires you to have Composer installed globally, as explained in the *installation chapter* of the Composer documentation.

> Liform follows the PSR-4 convention names for its classes, which means you can easily integrate `Liform` classes loading in your own autoloader.

#### Note

[](#note)

`symfony/form ^5.0` broke backwards compatibility on some abstract functions we use. If you need to function with earlier versions, you need to use Liform v0.15 or earlier:

```
$ composer require limenius/liform "^0.15"

```

Usage
-----

[](#usage)

Serializing a form into JSON Schema:

```
use Limenius\Liform\Resolver;
use Limenius\Liform\Liform;
use Limenius\Liform\Liform\Transformer;

$resolver = new Resolver();
$resolver->setTransformer('text', Transformer\StringTransformer);
$resolver->setTransformer('textarea', Transformer\StringTransformer, 'textarea');
// more transformers you might need, for a complete list of what is used in Symfony
// see https://github.com/Limenius/LiformBundle/blob/master/Resources/config/transformers.xml
$liform = new Liform($resolver);

$form = $this->createForm(CarType::class, $car, ['csrf_protection' => false]);
$schema = json_encode($liform->transform($form));
```

And `$schema` will contain a JSON Schema representation such as:

```
{
   "title":null,
   "properties":{
      "name":{
         "type":"string",
         "title":"Name",
         "propertyOrder":1
      },
      "color":{
         "type":"string",
         "title":"Color",
         "attr":{
            "placeholder":"444444"
         },
         "description":"3 hexadecimal digits",
         "propertyOrder":2
      },
      "drivers":{
         "type":"array",
         "title":"hola",
         "items":{
            "title":"Drivers",
            "properties":{
               "firstName":{
                  "type":"string",
                  "propertyOrder":1
               },
               "familyName":{
                  "type":"string",
                  "propertyOrder":2
               }
            },
            "required":[
               "firstName",
               "familyName"
            ],
            "type":"object"
         },
         "propertyOrder":3
      }
   },
   "required":[
      "name",
      "drivers"
   ]
}
```

Using your own transformers
---------------------------

[](#using-your-own-transformers)

Liform works by inspecting the form recursively, finding (resolving) the right transformer for every child and using that transformer to build the corresponding slice of the json-schema. So, if you want to modify the way a particular form type is transformed, you should set a transformer that matches a type with that `block_prefix`.

To do so, you can use the `setTransformer` method of the `Resolver` class. In this case we are reusing the StringTransformer, by overriding the widget property and setting it to `my_widget`, but you could use your very own transformer if you like:

```
use Limenius\Liform\Liform;

$stringTransformer = $this->get('liform.transformer.string');

$resolver = $this->get('liform.resolver');
$resolver->setTransformer('file', $stringTransformer, 'file_widget');
$liform = new Liform($resolver);
```

Serializing initial values
--------------------------

[](#serializing-initial-values)

This library provides a normalizer to serialize a `FormView` (you can create one with `$form->createView()`) into an array of initial values.

```
use Limenius\Liform\Serializer\Normalizer\FormViewNormalizer;

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new FormViewNormalizer());

$serializer = new Serializer($normalizers, $encoders);
$initialValues = $serializer->normalize($form),
```

To obtain an array of initial values that match your json-schema.

Serializing errors
------------------

[](#serializing-errors)

This library provides a normalizer to serialize forms with errors into an array. This part was shamelessly taken from [FOSRestBundle](https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Serializer/Normalizer/FormErrorNormalizer.php). To use this feature copy the following code in your controller action:

```
use Limenius\Liform\Serializer\Normalizer\FormErrorNormalizer;

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new FormErrorNormalizer());

$serializer = new Serializer($normalizers, $encoders);
$errors = $serializer->normalize($form),
```

To obtain an array with the errors of your form. [liform-react](https://github.com/Limenius/liform-react), if you are using it, can understand this format.

Information extracted to JSON-schema
------------------------------------

[](#information-extracted-to-json-schema)

The goal of Liform is to extract as much data as possible from the form in order to have a complete representation with validation and UI hints in the schema. The options currently supported are.

Some of the data can be extracted from the usual form attributes, however, some attributes will be provided using a special `liform` array that is passed to the form options. To do so in a comfortable way a [form extension](http://symfony.com/doc/current/form/create_form_type_extension.html) is provided. See [AddLiformExtension.php](https://github.com/Limenius/Liform/blob/master/src/Limenius/Liform/Form/Extension/AddLiformExtension.php)

### Required

[](#required)

If the field is required (which is the default in Symfony), it will be reflected in the schema.

```
class DummyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('someText', Type\TextType::class);
    }
}
```

```
{
   "title":"dummy",
   "type":"object",
   "properties":{
      "someText":{
         "type":"string",
         "title":"someText",
         "propertyOrder":1
      }
   },
   "required":[
      "someText"
   ]
}
```

### Widget

[](#widget)

Sometimes you might want to render a field differently then the default behaviour for that type. By using the liform attributes you can specify a particular widget that determines how this field is rendered.

If the attribute `widget` of `liform` is provided, as in the following code:

```
class DummyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('someText', Type\TextType::class, [
                'liform' => [
                    'widget' => 'my_widget'
                ]
            ]);
    }
}
```

The schema generated will have that `widget` option:

```
{
   "title":"dummy",
   "type":"object",
   "properties":{
      "someText":{
         "type":"string",
         "widget":"my_widget",
         "title":"someText",
         "propertyOrder":1
      }
   },
   "required":[
      "someText"
   ]
}
```

### Label/Title

[](#labeltitle)

If you provide a `label`, it will be used as title in the schema.

```
class DummyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('someText', Type\TextType::class, [
                'label' => 'Some text',
            ]);
    }
}
```

```
{
   "title":"dummy",
   "type":"object",
   "properties":{
      "someText":{
         "type":"string",
         "title":"Some text",
         "propertyOrder":1
      }
   },
   "required":[
      "someText"
   ]
}
```

### Pattern

[](#pattern)

If the attribute `pattern` of `attr` is provided, as in the following code:

```
class DummyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('someText', Type\TextType::class, [
                'attr' => [
                    'pattern' => '.{5,}',
                ],
            ]);
    }
}
```

It will be extracted as the `pattern` option, so it can be used for validation. Note that, in addition, everything provided to `attr` will be preserved as well.

```
{
   "title":"dummy",
   "type":"object",
   "properties":{
      "someText":{
         "type":"string",
         "title":"someText",
         "attr":{
            "pattern":".{5,}"
         },
         "pattern":".{5,}",
         "propertyOrder":1
      }
   },
   "required":[
      "someText"
   ]
}
```

### Description

[](#description)

If the attribute `description` of `liform` is provided, as in the following code, it will be extracted in the schema:

```
class DummyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('someText', Type\TextType::class, [
                'label' => 'Some text',
                'liform' => [
                    'description' => 'This is a help message',
                ]
            ]);
    }
}
```

```
{
   "title":"dummy",
   "type":"object",
   "properties":{
      "someText":{
         "type":"string",
         "title":"Some text",
         "description":"This is a help message",
         "propertyOrder":1
      }
   },
   "required":[
      "someText"
   ]
}
```

License
-------

[](#license)

This library is under the MIT license. See the complete license in the file:

```
LICENSE.md

```

Acknoledgements
---------------

[](#acknoledgements)

The technique for transforming forms using resolvers and reducers is inspired on [Symfony Console Form](https://github.com/matthiasnoback/symfony-console-form)

Testing
-------

[](#testing)

```
# Install dependencies
composer install

# Run all tests
vendor/bin/phpunit

# Run a single file
vendor/bin/phpunit tests/Path/To/FooTest.php

# Run a specific method
vendor/bin/phpunit --filter testMethodName
```

### Via Swan host app

[](#via-swan-host-app)

Tests also run through the Swan Docker container (from the Swan project root):

```
docker compose exec swan-php vendor/bin/phpunit --configuration vendor/starx/symfony-json-form/phpunit.xml.dist

# Single file
docker compose exec swan-php vendor/bin/phpunit vendor/starx/symfony-json-form/tests/Path/To/FooTest.php

# Specific method
docker compose exec swan-php vendor/bin/phpunit --filter testMethodName --configuration vendor/starx/symfony-json-form/phpunit.xml.dist
```

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance63

Regular maintenance activity

Popularity4

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 74.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~84 days

Recently: every ~65 days

Total

17

Last Release

2041d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d62aa9ddd1758dd002c3b34d8524c0ec0058b89f73544b5ffcdba7ac3ca491e5?d=identicon)[starx](/maintainers/starx)

---

Top Contributors

[![nacmartin](https://avatars.githubusercontent.com/u/154258?v=4)](https://github.com/nacmartin "nacmartin (78 commits)")[![luispabon](https://avatars.githubusercontent.com/u/6388823?v=4)](https://github.com/luispabon "luispabon (10 commits)")[![starx](https://avatars.githubusercontent.com/u/1029450?v=4)](https://github.com/starx "starx (5 commits)")[![nico-incubiq](https://avatars.githubusercontent.com/u/11662249?v=4)](https://github.com/nico-incubiq "nico-incubiq (2 commits)")[![sjopet](https://avatars.githubusercontent.com/u/881246?v=4)](https://github.com/sjopet "sjopet (2 commits)")[![Invis1ble](https://avatars.githubusercontent.com/u/1710944?v=4)](https://github.com/Invis1ble "Invis1ble (2 commits)")[![xabbuh](https://avatars.githubusercontent.com/u/1957048?v=4)](https://github.com/xabbuh "xabbuh (1 commits)")[![jrbarnard](https://avatars.githubusercontent.com/u/6758366?v=4)](https://github.com/jrbarnard "jrbarnard (1 commits)")[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (1 commits)")[![kermorgant](https://avatars.githubusercontent.com/u/10561580?v=4)](https://github.com/kermorgant "kermorgant (1 commits)")[![ThePolkaDotRook](https://avatars.githubusercontent.com/u/37351634?v=4)](https://github.com/ThePolkaDotRook "ThePolkaDotRook (1 commits)")[![grimpows](https://avatars.githubusercontent.com/u/18550672?v=4)](https://github.com/grimpows "grimpows (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/starx-symfony-json-form/health.svg)

```
[![Health](https://phpackages.com/badges/starx-symfony-json-form/health.svg)](https://phpackages.com/packages/starx-symfony-json-form)
```

###  Alternatives

[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3689.4k10](/packages/netgen-layouts-core)[jbtronics/settings-bundle

A symfony bundle to easily create typesafe, user-configurable settings for symfony applications

9546.7k2](/packages/jbtronics-settings-bundle)[symfony/ux-toggle-password

Toggle visibility of password inputs for Symfony Forms

26508.0k5](/packages/symfony-ux-toggle-password)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14112.1k8](/packages/netgen-content-browser)[mapbender/mapbender

Mapbender library

10117.4k5](/packages/mapbender-mapbender)[leapt/core-bundle

Symfony LeaptCoreBundle

2529.1k4](/packages/leapt-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
