Install the Bundle
composer require limenius/jsonform-bundle
Ensure your config/bundles.php includes:
return [
// ...
Limenius\JsonFormBundle\LimeniusJsonFormBundle::class => ['all' => true],
];
Configure the Bundle
Add to config/packages/limenius_jsonform.yaml:
limenius_jsonform:
enabled: true
routes:
schema: true # Enables `/_schema/{form_name}`
First Use Case: Generate JSON Schema for a Form
Define a Symfony form (e.g., src/Form/ExampleType.php):
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ExampleType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name', TextType::class)
->add('email', EmailType::class);
}
}
Access the schema via:
/_schema/example
Returns a JSON Schema for the form, usable in jsonform-react or json-editor.
Backend Form Definition
Define forms in Symfony as usual (e.g., UserType, ProductType). The bundle auto-generates schemas for all registered forms.
Frontend Integration
Fetch schemas via API (e.g., /_schema/user):
fetch('/_schema/user')
.then(res => res.json())
.then(schema => {
// Use with jsonform-react
const { JsonForm } = require('jsonform-react');
return <JsonForm schema={schema} />;
});
Validation & Submission
Use the schema for client-side validation (e.g., with ajv). Submit data to Symfony’s form handler:
// Controller
public function submit(ExampleType $form, Request $request) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Process data
}
}
FormFactory to generate schemas for dynamic forms:
$form = $this->createForm(ExampleType::class);
$schema = $this->get('limenius_jsonform.schema_generator')->generate($form);
Liform\Liform to support custom Symfony field types. Example:
use Liform\Liform;
use Limenius\JsonFormBundle\Extension\FieldExtensionInterface;
class CustomFieldExtension implements FieldExtensionInterface {
public function extend(Liform $liform) {
$liform->addType('custom_field', function ($type) {
return [
'type' => 'string',
'title' => $type->getOption('label'),
'customProperty' => true,
];
});
}
}
Register in services.yaml:
services:
App\Extension\CustomFieldExtension:
tags: [limenius_jsonform.field_extension]
Schema Caching
php bin/console cache:clear
config/packages/limenius_jsonform.yaml:
limenius_jsonform:
cache: false
Form Name Routing
/_schema/{form_name} uses the form type’s short class name (e.g., ExampleType → example).options['schema_route_name'] in the form type:
class ExampleType extends AbstractType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'schema_route_name' => 'custom_schema_name',
]);
}
}
Complex Field Types
Liform’s extend() method to customize:
$liform->addType('collection', function ($type) {
return [
'type' => 'array',
'items' => $type->getInnerType()->getSchema(),
];
});
config/packages/limenius_jsonform.yaml:
limenius_jsonform:
debug: true
Check logs for generation details.limenius_jsonform.schema_generator:
services:
App\Service\CustomSchemaGenerator:
tags: [limenius_jsonform.schema_generator]
limenius_jsonform.schema_processor event listener to modify schemas:
use Limenius\JsonFormBundle\Event\SchemaEvent;
public function onSchemaProcess(SchemaEvent $event) {
$event->getSchema()['customProperty'] = 'value';
}
Register as a service:
services:
App\EventListener\SchemaListener:
tags: [kernel.event_listener, { event: limenius_jsonform.schema_process, method: onSchemaProcess }]
jsonform-react is configured to handle Symfony’s schema structure. Example:
<JsonForm
schema={schema}
uischema={{
'ui:order' => ['name', 'email'],
}}
/>
properties. Use DataTransformer or Normalizer in Symfony to handle mismatches.How can I help you explore Laravel packages today?