survos / liform-bundle
Bundle for Liform: Transform Symfony Forms into Json Schema
Fund package maintenance!
kbond
Installs: 355
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.2
- survos/liform: ^1.5
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.8
- dev-main
- 1.5.378
- 1.5.377
- 1.5.376
- 1.5.375
- 1.5.374
- 1.5.373
- 1.5.372
- 1.5.371
- 1.5.370
- 1.5.369
- 1.5.368
- 1.5.367
- 1.5.366
- 1.5.365
- 1.5.364
- 1.5.363
- 1.5.362
- 1.5.361
- 1.5.360
- 1.5.359
- 1.5.358
- 1.5.357
- 1.5.356
- 1.5.355
- 1.5.354
- 1.5.353
- 1.5.352
- 1.5.351
- 1.5.350
- 1.5.345
- 1.5.344
- 1.5.343
- 1.5.342
- 1.5.341
- 1.5.340
- 1.5.339
- 1.5.338
- 1.5.337
- 1.5.336
- 1.5.335
- 1.5.334
- 1.5.333
- 1.5.332
- 1.5.331
- 1.5.330
- 1.5.329
- 1.5.328
- 1.5.327
- 1.5.326
- 1.5.325
- 1.5.324
- 1.5.323
- 1.5.322
- 1.5.321
- 1.5.320
- 1.5.319
- 1.5.318
- 1.5.317
- 1.5.316
- 1.5.315
- 1.5.314
- 1.5.313
- 1.5.312
- 1.5.311
- 1.5.310
- 1.5.309
- 1.5.308
- 1.5.307
- 1.5.306
- 1.5.305
- 1.5.304
- 1.5.303
- 1.5.302
- 1.5.301
- 1.5.300
- 1.5.299
- 1.5.298
- 1.5.297
- 1.5.296
- 1.5.295
- 1.5.294
- 1.5.293
- 1.5.292
- 1.5.291
- 1.5.290
- 1.5.289
- 1.5.288
- 1.5.287
- 1.5.286
- 1.5.285
- 1.5.284
- 1.5.283
- 1.5.282
- 1.5.281
- 1.5.280
- 1.5.279
- 1.5.278
- 1.5.277
- 1.5.276
- 1.5.275
- 1.5.274
- 1.5.273
- 1.5.272
- 1.5.271
- 1.5.270
- 1.5.269
- 1.5.268
- 1.5.267
- 1.5.266
- 1.5.265
- 1.5.264
- 1.5.263
- 1.5.262
- 1.5.261
- 1.5.260
- 1.5.259
- 1.5.258
- 1.5.257
- 1.5.256
- 1.5.255
- 1.5.254
- 1.5.253
- 1.5.252
- 1.5.251
- 1.5.250
- 1.5.249
- 1.5.248
- 1.5.247
- 1.5.246
- 1.5.245
- 1.5.244
- 1.5.243
- 1.5.242
- 1.5.241
- 1.5.240
- 1.5.239
- 1.5.238
- 1.5.237
- 1.5.236
- 1.5.235
- 1.5.234
- 1.5.233
- 1.5.232
- 1.5.231
- 1.5.230
- 1.5.229
- 1.5.228
- 1.5.227
- 1.5.226
- 1.5.225
- 1.5.224
- 1.5.223
- 1.5.222
- 1.5.221
- 1.5.220
- 1.5.219
- 1.5.218
- 1.5.217
- 1.5.216
- 1.5.215
- 1.5.214
- 1.5.213
- 1.5.212
- 1.5.211
- 1.5.210
- 1.5.209
- 1.5.208
- 1.5.207
- 1.5.206
- 1.5.205
- 1.5.204
- 1.5.203
- 1.5.202
- 1.5.201
- 1.5.200
- 1.5.199
- 1.5.198
- 1.5.197
- 1.5.196
- 1.5.195
- 1.5.194
- 1.5.193
- 1.5.192
- 1.5.191
- 1.5.190
- 1.5.189
- 1.5.188
- 1.5.187
- 1.5.186
- 1.5.185
- 1.5.184
- 1.5.183
- 1.5.182
- 1.5.181
- 1.5.180
- 1.5.179
- 1.5.178
- 1.5.177
- 1.5.176
- 1.5.175
- 1.5.174
- 1.5.173
- 1.5.172
- 1.5.171
- 1.5.170
- 1.5.169
- 1.5.168
- 1.5.167
- 1.5.166
- 1.5.165
- 1.5.164
- 1.5.163
- 1.5.162
- 1.5.161
- 1.5.160
- 1.5.159
- 1.5.158
- 1.5.157
- 1.5.156
- 1.5.155
- 1.5.154
- 1.5.153
- 1.5.152
- 1.5.151
- 1.5.150
- 1.5.149
- 1.5.148
- 1.5.147
- 1.5.146
- 1.5.145
- 1.5.144
- 1.5.143
- 1.5.142
- 1.5.141
- 1.5.140
- 1.5.139
- 1.5.138
- 1.5.137
- 1.5.136
- 1.5.135
- 1.5.134
- 1.5.133
- 1.5.132
- 1.5.131
- 1.5.130
- 1.5.129
- 1.5.128
- 1.5.127
- 1.5.126
- 1.5.125
- 1.5.124
- 1.5.123
- 1.5.122
- 1.5.121
- 1.5.120
- 1.5.119
- 1.5.118
- 1.5.117
- 1.5.116
- 1.5.115
- 1.5.114
- 1.5.113
- 1.5.112
- 1.5.111
- 1.5.110
- 1.5.109
- 1.5.108
- 1.5.107
- 1.5.106
- 1.5.105
- 1.5.104
- 1.5.103
- 1.5.102
- 1.5.101
- 1.5.100
- 1.5.99
- 1.5.98
- 1.5.97
- 1.5.96
- 1.5.95
- 1.5.94
- 1.5.93
- 1.5.92
- 1.5.91
- 1.5.90
- 1.5.89
- 1.5.88
- 1.5.87
- 1.5.86
- 1.5.85
- 1.5.84
- 1.5.83
- 1.5.82
- 1.5.81
- 1.5.80
- 1.5.79
- 1.5.78
- 1.5.77
- 1.5.76
- 1.5.75
- 1.5.74
- 1.5.73
- 1.5.72
- 1.5.71
- 1.5.70
- 1.5.69
- 1.5.68
- 1.5.67
- 1.5.66
- 1.5.65
- 1.5.64
- 1.5.63
- 1.5.62
- 1.5.61
- 1.5.60
- 1.5.59
- 1.5.58
- 1.5.57
- 1.5.56
- 1.5.55
- 1.5.54
- 1.5.53
- 1.5.52
- 1.5.51
- 1.5.50
- 1.5.49
- 1.5.48
- 1.5.47
- 1.5.46
- 1.5.45
- 1.5.44
- 1.5.43
- 1.5.42
- 1.5.41
- 1.5.40
- 1.5.39
- 1.5.38
- 1.5.37
- 1.5.36
- 1.5.35
- 1.5.34
- 1.5.33
- 1.5.32
- 1.5.31
- 1.5.30
- 1.5.29
- 1.5.28
- 1.5.27
- 1.5.26
- 1.5.25
- 1.5.24
- 1.5.23
- 1.5.22
- 1.5.21
- 1.5.20
- 1.5.19
- 1.5.18
- 1.5.17
- 1.5.16
- 1.5.15
- 1.5.14
- 1.5.13
- 1.5.12
- 1.5.11
- 1.5.10
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.103
- 1.4.102
- 1.4.101
- 1.4.100
- 1.4.99
- 1.4.98
- 1.4.97
- 1.4.96
- 1.4.95
- 1.4.94
- 1.4.93
- 1.4.92
- 1.4.91
- 1.4.90
- 1.4.89
- 1.4.88
- 1.4.87
- 1.4.86
- 1.4.85
- 1.4.84
- 1.4.83
- 1.4.82
- 1.4.81
- 1.4.80
- 1.4.79
- 1.4.78
- 1.4.77
- 1.4.76
- 1.4.75
- 1.4.74
- 1.4.73
- 1.4.72
- 1.4.71
- 1.4.70
- 1.4.69
- 1.4.68
- 1.4.67
- 1.4.66
- 1.4.65
- 1.4.64
- 1.4.63
- 1.4.62
- 1.4.61
- 1.4.60
- 1.4.59
- 1.4.58
- 1.4.57
- 1.4.56
- 1.4.55
- 1.4.54
- 1.4.53
- 1.4.52
- 1.4.51
- 1.4.50
- 1.4.49
- 1.4.48
- 1.4.44
- 1.4.43
- 1.4.42
- 1.4.41
- 1.4.40
- 1.4.39
This package is auto-updated.
Last update: 2024-11-04 17:03:23 UTC
README
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.
Install 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.
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