All Projects → Limenius → Liformbundle

Limenius / Liformbundle

Licence: mit
Symfony Bundle to render Symfony Forms to JSON Schema

Projects that are alternatives of or similar to Liformbundle

Liform
PHP library to render Symfony Forms to JSON Schema
Stars: ✭ 113 (-8.87%)
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 (+59.68%)
Mutual labels:  symfony, symfony-bundle, forms
React Jsonschema Form
A React component for building Web forms from JSON Schema.
Stars: ✭ 10,870 (+8666.13%)
Mutual labels:  json-schema, forms
Knppaginatorbundle
SEO friendly Symfony paginator to sort and paginate
Stars: ✭ 1,534 (+1137.1%)
Mutual labels:  symfony, symfony-bundle
Slack Bundle
Symfony bundle integration of the nexylan/slack library.
Stars: ✭ 110 (-11.29%)
Mutual labels:  symfony, symfony-bundle
Filemanagerbundle
FileManager is a simple Multilingual File Manager Bundle for Symfony
Stars: ✭ 105 (-15.32%)
Mutual labels:  symfony, symfony-bundle
Notification Bundle
A simple Symfony bundle to notify user
Stars: ✭ 103 (-16.94%)
Mutual labels:  symfony, symfony-bundle
Liipimaginebundle
Symfony Bundle to assist in imagine manipulation using the imagine library
Stars: ✭ 1,516 (+1122.58%)
Mutual labels:  symfony, symfony-bundle
Easy Security Bundle
EasySecurityBundle
Stars: ✭ 95 (-23.39%)
Mutual labels:  symfony, symfony-bundle
Core Bundle
[READ-ONLY] Contao Core Bundle
Stars: ✭ 113 (-8.87%)
Mutual labels:  symfony, symfony-bundle
Swiftmailer Bundle
Symfony Swiftmailer Bundle
Stars: ✭ 1,558 (+1156.45%)
Mutual labels:  symfony, symfony-bundle
Vichuploaderbundle
A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
Stars: ✭ 1,613 (+1200.81%)
Mutual labels:  symfony, symfony-bundle
Fosjsroutingbundle
A pretty nice way to expose your Symfony2 routing to client applications.
Stars: ✭ 1,358 (+995.16%)
Mutual labels:  symfony, symfony-bundle
Sonataseobundle
Symfony SonataSeoBundle
Stars: ✭ 106 (-14.52%)
Mutual labels:  symfony, symfony-bundle
Easy Doc Bundle
Symfony application documentation generator
Stars: ✭ 99 (-20.16%)
Mutual labels:  symfony, symfony-bundle
Liipcachecontrolbundle
DEPRECATED! This bundle is superseded by FOSHttpCacheBundle. A migration guide is in the README of LiipCacheControlBundle
Stars: ✭ 108 (-12.9%)
Mutual labels:  symfony, symfony-bundle
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (-8.06%)
Mutual labels:  symfony, symfony-bundle
Whiteoctobertcpdfbundle
A bundle to facilitate using TCPDF for PDF generation in Symfony applications
Stars: ✭ 91 (-26.61%)
Mutual labels:  symfony, symfony-bundle
Event Store Symfony Bundle
Event Store Symfony Bundle
Stars: ✭ 93 (-25%)
Mutual labels:  symfony, symfony-bundle
Crauegeobundle
Doctrine functions for calculating geographical distances in your Symfony project.
Stars: ✭ 112 (-9.68%)
Mutual labels:  symfony, symfony-bundle

LiformBundle

Bundle that integrates Liform into Symfony. Liform is a library to serialize Symfony Forms into JSON schema. For use with liform-react or json-editor, or any other form generator based on json-schema.

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

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

Installation

First and foremost, note that you have a complete example with React, Webpack and Symfony Standard Edition at Limenius/symfony-react-sandbox ready for you, which includes an example implementation of this bundle.

Feel free to clone it, run it, experiment, and copy the pieces you need to your project. Because this bundle focuses mainly on the frontend side of things, you are expected to have a compatible frontend setup.

Step 1: Download the Bundle

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

$ composer require limenius/liform-bundle

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

Step 2: Enable the Bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Limenius\LiformBundle\LimeniusLiformBundle(),
        );

        // ...
    }

    // ...
}

Usage

Serializing a form into JSON Schema:

        $form = $this->createForm(CarType::class, $car, ['csrf_protection' => false]);
        $schema = json_encode($this->get('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"
         },
         "default":"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"
   ]
}

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.

Check out the Liform documentation for more details.

Using your own transformers

Liform works by recursively inspecting the form, 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 can add a transformer and configure it to to be applied for all children with a particular block_prefix.
To achieve this, you should create a new service definition and add the liform.transformer tag. You need to specify for which form-types your transformer will be applied by setting the form_type property of the tag to the corresponding block_prefix.

In the following example we are reusing the StringTransformer class. By specifying the widget property of the tag we can scope the transformer to only work for types with that particular widget.

services:
    app.liform.file_type.transformer:
        class: "%liform.transformer.string.class%"
        parent: Limenius\Liform\Transformer\AbstractTransformer
        tags:
            - { name: liform.transformer, form_type: file, widget: file_widget }

You can of course use your very own Transformer class, just make sure to implement the required Limenius\Liform\Transformer\TransformerInterface when you do.

Extending the default behaviour

In addition to adding your own transformers for customizing the serialization of a specific form-type Liform allows you to add extensions to customize the default behaviour of all types.
In the following example we use an Extension to add a submit_url property to the schema representing the form's action parameter.

<?php

use Limenius\Liform\Transformer\ExtensionInterface;
use Symfony\Component\Form\FormInterface;

class FormDataExtension implements ExtensionInterface
{
    /**
     * @param FormInterface $form
     * @param array         $schema
     *
     * @return array
     */
    public function apply(FormInterface $form, array $schema)
    {
        if (!$form->isRoot()) {
            return $schema;
        }

        if (!$form->getConfig()->hasOption('action')) {
            return $schema;
        }

        $schema['submit_url'] = $form->getConfig()->getOption('action');

        return $schema;
    }
}

Make sure your Extension class implements the required Limenius\Liform\Transformer\ExtensionInterface. To register your extension; create a new service definition and add the liform.extension tag to it.

services:
    app.liform.form_data.extension:
        class: MyProject\Application\Liform\FormDataExtension
        tags:
            - { name: liform.extension }

Serializing initial values

This bundle registers a normalizer to serialize a FormView class into an array of initial values that match your json-schema. The following example shows you how to use this feature in a controller action:

$serializer = $this->get('serializer');
$initialValues = $serializer->normalize($form);

Serializing errors

This bundle registers a normalizer to serialize forms with errors into an array. This part was shamelessly taken from FOSRestBundle. Copy the following statements to use this feature:

$serializer = $this->get('serializer');
$errors = $serializer->normalize($form);

The format of the array containing the normalized form errors is compatible with the liform-react package.

License

This bundle was released under the MIT license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code.

LICENSE.md

Acknowledgements

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