Download the PHP package answear/liform without Composer
On this page you can find all versions of the php package answear/liform. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download answear/liform
More information about answear/liform
Files in answear/liform
Package liform
Short Description Library to transform Symfony Forms into Json Schema
License MIT
Informations about the package liform
Liform
Liform is a library for serializing Symfony Forms into JSON schema. It can be used along with liform-react or json-editor, or any other form generator based on json-schema.
It is used by 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.
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
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
Serializing a form into JSON Schema:
And $schema
will contain a JSON Schema representation such as:
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:
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.
To obtain an array of initial values that match your json-schema.
Serializing errors
This library provides a normalizer to serialize forms with errors into an array. This part was shamelessly taken from FOSRestBundle. To use this feature copy the following code in your controller action:
To obtain an array with the errors of your form. liform-react, if you are using it, can understand this format.
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 is provided. See AddLiformExtension.php
Required
If the field is required (which is the default in Symfony), it will be reflected in the schema.
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:
The schema generated will have that widget
option:
Label/Title
If you provide a label
, it will be used as title in the schema.
Pattern
If the attribute pattern
of attr
is provided, as in the following code:
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.
Description
If the attribute description
of liform
is provided, as in the following code, it will be extracted in the schema:
License
This library is under the MIT license. See the complete license in the file:
LICENSE.md
Acknoledgements
The technique for transforming forms using resolvers and reducers is inspired on Symfony Console Form
All versions of liform with dependencies
symfony/form Version ^6.0|^7.0
symfony/translation Version ^6.0|^7.0
symfony/serializer Version ^6.0|^7.0
symfony/validator Version ^6.0|^7.0