{"id":15233399,"url":"https://github.com/limenius/liform","last_synced_at":"2025-04-14T20:45:30.033Z","repository":{"id":16734099,"uuid":"80370933","full_name":"Limenius/Liform","owner":"Limenius","description":"PHP library to render Symfony Forms to JSON Schema","archived":false,"fork":false,"pushed_at":"2025-02-21T12:25:10.000Z","size":156,"stargazers_count":140,"open_issues_count":14,"forks_count":77,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-12T16:44:43.369Z","etag":null,"topics":["forms","json-schema","php","symfony"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Limenius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-29T20:36:13.000Z","updated_at":"2025-02-28T22:42:00.000Z","dependencies_parsed_at":"2024-06-18T12:32:02.277Z","dependency_job_id":"2ea938c4-c16e-42c3-9efd-0f06d33af0b3","html_url":"https://github.com/Limenius/Liform","commit_stats":{"total_commits":122,"total_committers":21,"mean_commits":5.809523809523809,"dds":0.4590163934426229,"last_synced_commit":"493eb0cc1f720607b6c882503c000851fde80e70"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Limenius%2FLiform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Limenius%2FLiform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Limenius%2FLiform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Limenius%2FLiform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Limenius","download_url":"https://codeload.github.com/Limenius/Liform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248960513,"owners_count":21189984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["forms","json-schema","php","symfony"],"created_at":"2024-09-29T05:08:29.275Z","updated_at":"2025-04-14T20:45:29.987Z","avatar_url":"https://github.com/Limenius.png","language":"PHP","readme":"Liform\n======\n\nThis fork also merges git@github.com:answear/Liform.git.\n\nLiform 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.\n\nIt is used by [LiformBundle](https://github.com/Limenius/LiformBundle) but can also be used as a stand-alone library.\n\nIt 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.\n\nLiform 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.\n\n[![Build Status](https://travis-ci.org/Limenius/Liform.svg?branch=master)](https://travis-ci.org/Limenius/Liform)\n[![Latest Stable Version](https://poser.pugx.org/limenius/liform/v/stable)](https://packagist.org/packages/limenius/liform)\n[![Latest Unstable Version](https://poser.pugx.org/limenius/liform/v/unstable)](https://packagist.org/packages/limenius/liform)\n[![License](https://poser.pugx.org/limenius/liform/license)](https://packagist.org/packages/limenius/liform)\n\n## Installation\n\nOpen a console, enter your project directory and execute the\nfollowing command to download the latest stable version of this library:\n\n```bash\ncomposer require survos/liform\n```\n\nThis command requires you to have Composer installed globally, as explained\nin the *installation chapter* of the Composer documentation.\n\n\u003e Liform follows the PSR-4 convention names for its classes, which means you can easily integrate `Liform` classes loading in your own autoloader.\n\n## Usage\n\nSerializing a form into JSON Schema:\n\n```php\nuse Limenius\\Liform\\Resolver;\nuse Limenius\\Liform\\Liform;\nuse Limenius\\Liform\\Liform\\Transformer;\n\n$resolver = new Resolver();\n$resolver-\u003esetTransformer('text', Transformer\\StringTransformer);\n$resolver-\u003esetTransformer('textarea', Transformer\\StringTransformer, 'textarea');\n// more transformers you might need, for a complete list of what is used in Symfony\n// see https://github.com/Limenius/LiformBundle/blob/master/Resources/config/transformers.xml\n$liform = new Liform($resolver);\n\n$form = $this-\u003ecreateForm(CarType::class, $car, ['csrf_protection' =\u003e false]);\n$schema = json_encode($liform-\u003etransform($form));\n```\n\nAnd `$schema` will contain a JSON Schema representation such as:\n\n```json\n{\n   \"title\":null,\n   \"properties\":{\n      \"name\":{\n         \"type\":\"string\",\n         \"title\":\"Name\",\n         \"propertyOrder\":1\n      },\n      \"color\":{\n         \"type\":\"string\",\n         \"title\":\"Color\",\n         \"attr\":{\n            \"placeholder\":\"444444\"\n         },\n         \"description\":\"3 hexadecimal digits\",\n         \"propertyOrder\":2\n      },\n      \"drivers\":{\n         \"type\":\"array\",\n         \"title\":\"hola\",\n         \"items\":{\n            \"title\":\"Drivers\",\n            \"properties\":{\n               \"firstName\":{\n                  \"type\":\"string\",\n                  \"propertyOrder\":1\n               },\n               \"familyName\":{\n                  \"type\":\"string\",\n                  \"propertyOrder\":2\n               }\n            },\n            \"required\":[\n               \"firstName\",\n               \"familyName\"\n            ],\n            \"type\":\"object\"\n         },\n         \"propertyOrder\":3\n      }\n   },\n   \"required\":[\n      \"name\",\n      \"drivers\"\n   ]\n}\n```\n\n## Using your own transformers\n\nLiform 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`.\n\nTo 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:\n\n```php\n\nuse Limenius\\Liform\\Liform;\n\n$stringTransformer = $this-\u003eget('liform.transformer.string');\n\n$resolver = $this-\u003eget('liform.resolver');\n$resolver-\u003esetTransformer('file', $stringTransformer, 'file_widget');\n$liform = new Liform($resolver);\n```\n\n## Serializing initial values\n\nThis library provides a normalizer to serialize a `FormView` (you can create one with `$form-\u003ecreateView()`) into an array of initial values.\n\n```php\nuse Limenius\\Liform\\Serializer\\Normalizer\\FormViewNormalizer;\n\n$encoders = array(new XmlEncoder(), new JsonEncoder());\n$normalizers = array(new FormViewNormalizer());\n\n$serializer = new Serializer($normalizers, $encoders);\n$initialValues = $serializer-\u003enormalize($form),\n```\n\nTo obtain an array of initial values that match your json-schema.\n\n\n## Serializing errors\n\n\nThis 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:\n\n```php\nuse Limenius\\Liform\\Serializer\\Normalizer\\FormErrorNormalizer;\n\n$encoders = array(new XmlEncoder(), new JsonEncoder());\n$normalizers = array(new FormErrorNormalizer());\n\n$serializer = new Serializer($normalizers, $encoders);\n$errors = $serializer-\u003enormalize($form),\n```\n\nTo 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.\n\n## Information extracted to JSON-schema\n\nThe 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.\n\nSome 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)\n\n### Required\n\nIf the field is required (which is the default in Symfony), it will be reflected in the schema.\n\n```php\nclass DummyType extends AbstractType\n{\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('someText', Type\\TextType::class);\n    }\n}\n```\n\n```json\n{\n   \"title\":\"dummy\",\n   \"type\":\"object\",\n   \"properties\":{\n      \"someText\":{\n         \"type\":\"string\",\n         \"title\":\"someText\",\n         \"propertyOrder\":1\n      }\n   },\n   \"required\":[\n      \"someText\"\n   ]\n}\n```\n\n### Widget\n\nSometimes 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.\n\n\nIf the attribute `widget` of `liform` is provided, as in the following code:\n\n```php\nclass DummyType extends AbstractType\n{\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('someText', Type\\TextType::class, [\n                'liform' =\u003e [\n                    'widget' =\u003e 'my_widget'\n                ]\n            ]);\n    }\n}\n```\n\nThe schema generated will have that `widget` option:\n```json\n{\n   \"title\":\"dummy\",\n   \"type\":\"object\",\n   \"properties\":{\n      \"someText\":{\n         \"type\":\"string\",\n         \"widget\":\"my_widget\",\n         \"title\":\"someText\",\n         \"propertyOrder\":1\n      }\n   },\n   \"required\":[\n      \"someText\"\n   ]\n}\n```\n\n### Label/Title\n\nIf you provide a `label`, it will be used as title in the schema.\n\n```php\nclass DummyType extends AbstractType\n{\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('someText', Type\\TextType::class, [\n                'label' =\u003e 'Some text',\n            ]);\n    }\n}\n```\n\n```json\n{\n   \"title\":\"dummy\",\n   \"type\":\"object\",\n   \"properties\":{\n      \"someText\":{\n         \"type\":\"string\",\n         \"title\":\"Some text\",\n         \"propertyOrder\":1\n      }\n   },\n   \"required\":[\n      \"someText\"\n   ]\n}\n```\n\n### Pattern\n\nIf the attribute `pattern` of `attr` is provided, as in the following code:\n\n```php\nclass DummyType extends AbstractType\n{\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('someText', Type\\TextType::class, [\n                'attr' =\u003e [\n                    'pattern' =\u003e '.{5,}',\n                ],\n            ]);\n    }\n}\n\n```\nIt 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.\n\n\n```json\n{\n   \"title\":\"dummy\",\n   \"type\":\"object\",\n   \"properties\":{\n      \"someText\":{\n         \"type\":\"string\",\n         \"title\":\"someText\",\n         \"attr\":{\n            \"pattern\":\".{5,}\"\n         },\n         \"pattern\":\".{5,}\",\n         \"propertyOrder\":1\n      }\n   },\n   \"required\":[\n      \"someText\"\n   ]\n}\n```\n\n### Description\n\nIf the attribute `description` of `liform` is provided, as in the following code, it will be extracted in the schema:\n\n```php\nclass DummyType extends AbstractType\n{\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('someText', Type\\TextType::class, [\n                'label' =\u003e 'Some text',\n                'liform' =\u003e [\n                    'description' =\u003e 'This is a help message',\n                ]\n            ]);\n    }\n}\n```\n\n```json\n{\n   \"title\":\"dummy\",\n   \"type\":\"object\",\n   \"properties\":{\n      \"someText\":{\n         \"type\":\"string\",\n         \"title\":\"Some text\",\n         \"description\":\"This is a help message\",\n         \"propertyOrder\":1\n      }\n   },\n   \"required\":[\n      \"someText\"\n   ]\n}\n```\n\n## License\n\nThis library is under the MIT license. See the complete license in the file:\n\n    LICENSE.md\n\n## Acknowledgements\n\nThe technique for transforming forms using resolvers and reducers is inspired on [Symfony Console Form](https://github.com/matthiasnoback/symfony-console-form)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimenius%2Fliform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flimenius%2Fliform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimenius%2Fliform/lists"}