Install the Bundle
composer require limenius/liform-bundle
Ensure your composer.json includes the bundle in the require section.
Enable the Bundle
Add Limenius\LiformBundle\LiformBundle to the bundles.php (Symfony 4+) or AppKernel.php (Symfony 3-).
Configure the Bundle
Add the following to config/packages/liform.yaml:
liform:
enabled: true
routes:
schema: true
form_types:
- 'App\Form\YourFormType'
Generate a Schema
Create a form type (e.g., src/Form/YourFormType.php) and annotate it with @Liform\Annotation\AsJsonSchema if needed. Access the schema via:
/_liform/schema/{form_type_name}
First Use Case
Use the generated schema in a frontend tool like liform-react or json-editor to render a form dynamically. Example:
import { Liform } from 'liform-react';
fetch('/_liform/schema/YourFormType')
.then(response => response.json())
.then(schema => <Liform schema={schema} />);
Schema Generation
@Liform\Annotation\AsJsonSchema to customize schema generation for specific fields or forms.
use Liform\Annotation\AsJsonSchema;
class YourFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('fieldName', TextType::class, [
'constraints' => [
new NotBlank(),
],
// Custom schema options
'liform' => [
'title' => 'Custom Field Label',
'description' => 'Custom description',
'pattern' => '^[A-Za-z0-9]+$',
],
]);
}
}
Frontend Integration
liform-react to render forms from the generated schema.
<Liform
schema={schema}
onChange={handleFormChange}
formData={initialData}
/>
ajv or json-schema libraries).Dynamic Form Handling
liform:
routes:
schema: true
form: true # Enable form rendering endpoint if needed
Documentation
/_liform/schema/{form_type_name}) as API documentation for frontend developers.Symfony Forms to JSON Schema
choices, constraints) to JSON Schema properties. For example:
NotBlank constraint → required: trueChoiceType with choices → enum or oneOf in schema.liform option in field definitions to override default behavior:
$builder->add('field', TextType::class, [
'liform' => [
'type' => 'string',
'format' => 'email',
],
]);
Custom Form Types
Liform\Type\AbstractType to support custom form types in schemas. Example:
use Liform\Type\AbstractType;
class CustomFormType extends AbstractType {
public function getName() {
return 'custom_form_type';
}
public function getSchema() {
return [
'type' => 'object',
'properties' => [
'customField' => [
'type' => 'string',
],
],
];
}
}
API Versioning
/api/v1/_liform/schema/{form_type_name}
Caching
config/packages/liform.yaml:
liform:
cache: true
cache_lifetime: 3600 # 1 hour
Frontend Compatibility
liform-react, json-editor) supports the JSON Schema draft version generated by Liform. Default is draft-07.Nested Forms and Collections
CollectionType, FormType inside other FormTypes) may require manual schema adjustments. Use liform option to override:
$builder->add('nestedForm', CollectionType::class, [
'entry_type' => NestedFormType::class,
'liform' => [
'items' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
],
],
],
]);
Circular References
FormType A includes FormType B, which includes FormType A). This can cause infinite loops in schema generation.Route Conflicts
/_liform/schema/{form_type_name}) might conflict with other routes. Customize in config/packages/liform.yaml:
liform:
routes:
schema: '/api/schemas/{form_type_name}'
Deprecated Features
Symfony 5.4+ features). Check compatibility with your Symfony version.Schema Validation
$schema = $this->get('liform.schema_generator')->generateSchema('YourFormType');
var_dump($schema);
Logging
config/packages/liform.yaml:
liform:
debug: true
var/log/dev.log) for schema generation errors.Frontend Debugging
Custom Schema Generators
Liform\Generator\SchemaGenerator to modify schema generation logic. Example:
namespace App\Liform;
use Liform\Generator\SchemaGenerator;
class CustomSchemaGenerator extends SchemaGenerator {
protected function getFieldSchema($field, $options) {
$schema = parent::getFieldSchema($field, $options);
// Custom logic here
return $schema;
}
}
services:
App\Liform\CustomSchemaGenerator:
tags: ['liform.schema_generator']
Event Listeners
liform.schema_generated event to post-process schemas:
use Liform\Event\SchemaGeneratedEvent;
use Symfony\Component\EventDispatcher\GenericEvent;
$dispatcher->addListener('liform.schema_generated', function (SchemaGeneratedEvent $event) {
$schema = $event->getSchema();
// Modify schema here
$event->setSchema($schema);
});
Twig Integration
{% set schema = app.liform.schema_generator.generateSchema('YourFormType') %}
<script>
const formSchema = {{ schema|json_encode|raw }};
</script>
API Platform Integration
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
json_schema:
mime_types: ['application/schema+json']
liform: true
How can I help you explore Laravel packages today?