All Projects → Limenius → Liform

Limenius / Liform

Licence: mit
PHP library to render Symfony Forms to JSON Schema

Projects that are alternatives of or similar to Liform

Liformbundle
Symfony Bundle to render Symfony Forms to JSON Schema
Stars: ✭ 124 (+9.73%)
Mutual labels:  json-schema, symfony, forms
Rich Model Forms Bundle
Provides additional data mappers that ease the use of the Symfony Form component with rich models.
Stars: ✭ 198 (+75.22%)
Mutual labels:  symfony, forms
React Jsonschema Form
A React component for building Web forms from JSON Schema.
Stars: ✭ 10,870 (+9519.47%)
Mutual labels:  json-schema, forms
Ngx Formly
JSON powered / Dynamic forms for Angular
Stars: ✭ 2,109 (+1766.37%)
Mutual labels:  json-schema, forms
Liform React
Generate forms from JSON Schema to use with React (& redux-form)
Stars: ✭ 167 (+47.79%)
Mutual labels:  json-schema, forms
Angular
JSON powered forms for Angular
Stars: ✭ 385 (+240.71%)
Mutual labels:  json-schema, forms
Jsonform
Build forms from JSON Schema. Easily template-able. Compatible with Bootstrap 3 out of the box.
Stars: ✭ 2,416 (+2038.05%)
Mutual labels:  json-schema, forms
Vue Form Json Schema
Create forms using JSON schema. Bring your components!
Stars: ✭ 253 (+123.89%)
Mutual labels:  json-schema, forms
React Json Editor
A dynamic form component for react using JSON-Schema.
Stars: ✭ 201 (+77.88%)
Mutual labels:  json-schema, forms
Symfony React Sandbox
Example of integration with React and Webpack (Webpack Encore) for universal (isomorphic) React rendering, using Limenius/ReactBundle and Limenius/LiformBundle
Stars: ✭ 325 (+187.61%)
Mutual labels:  json-schema, symfony
Jsonforms
Customizable JSON Schema-based forms with React, Angular and Vue support out of the box.
Stars: ✭ 542 (+379.65%)
Mutual labels:  json-schema, forms
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (+1357.52%)
Mutual labels:  json-schema
Liipcachecontrolbundle
DEPRECATED! This bundle is superseded by FOSHttpCacheBundle. A migration guide is in the README of LiipCacheControlBundle
Stars: ✭ 108 (-4.42%)
Mutual labels:  symfony
Polyfill Php56
This component provides functions unavailable in releases prior to PHP 5.6.
Stars: ✭ 1,470 (+1200.88%)
Mutual labels:  symfony
React Forms
React library for rendering forms.
Stars: ✭ 111 (-1.77%)
Mutual labels:  forms
Personal Management System
Your web application for managing personal data. <[email protected]>
Stars: ✭ 2,027 (+1693.81%)
Mutual labels:  symfony
Knppaginatorbundle
SEO friendly Symfony paginator to sort and paginate
Stars: ✭ 1,534 (+1257.52%)
Mutual labels:  symfony
Sonataseobundle
Symfony SonataSeoBundle
Stars: ✭ 106 (-6.19%)
Mutual labels:  symfony
Pypdftk
Python module to drive the awesome pdftk binary.
Stars: ✭ 107 (-5.31%)
Mutual labels:  forms
Clone Section Of Form Es6 Or Jquery
Now on npm. Using vanilla JavaScript (ES6) or jQuery to duplicate a section of a form, maintaining accessibility (a11y).
Stars: ✭ 112 (-0.88%)
Mutual labels:  forms

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.

Build Status Latest Stable Version Latest Unstable Version License

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:

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

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

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

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:

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, 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.

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

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

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

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

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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].