## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require limenius/liform:^2.0
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"App\\": "app/",
"Limenius\\Liform\\": "vendor/limenius/liform/src/"
}
}
Run composer dump-autoload.
First Use Case: Convert a Form to JSON Schema
use Limenius\Liform\Liform;
use Symfony\Component\Form\FormBuilderInterface;
// Build a Symfony Form
$builder = $this->formFactory->createNamedBuilder('user', null, null);
$builder->add('name', TextType::class);
$builder->add('email', EmailType::class);
$builder->add('age', IntegerType::class);
$form = $builder->getForm();
// Convert to JSON Schema (updated for 2.0+)
$schema = Liform::create()->fromForm($form)->toArray();
Where to Look First
src/Limenius/Liform/Liform.php for core functionality.src/Limenius/Liform/Builder/ for form type-specific transformations.AbstractTransformer class for understanding isRequired() behavior (2.0.1+).// In a controller or service (2.0+ syntax)
public function getFormSchema(FormBuilderInterface $builder)
{
$form = $builder->getForm();
return Liform::create()
->fromForm($form)
->toArray();
}
// Access the AbstractTransformer's isRequired() logic for custom checks
$transformer = Liform::create()->fromForm($form)->getTransformer('name');
$isRequired = $transformer->isRequired(); // Returns boolean (new in 2.0.1)
Liform::create()
->fromForm($form)
->setOption('required_all_when_empty', true)
->setOption('use_full_object_key', true)
->toArray();
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
class FormSchemaTransformer implements DataTransformerInterface
{
public function transform($object, string $to, array $context = [])
{
$form = $this->formFactory->createNamedBuilder($object->getFormType(), null, $object->getData())
->getForm();
return Liform::create()->fromForm($form)->toArray();
}
}
// Example: Form with a collection of items
$builder->add('items', CollectionType::class, [
'entry_type' => TextType::class,
'allow_add' => true,
'allow_delete' => true,
]);
// Liform automatically handles nested schemas for collections
$schema = Liform::create()->fromForm($form)->toArray();
// Result: `items` will be an array with `type: "array"` and `items: { ... }`
Service Provider Binding (Updated)
Bind Liform as a singleton in AppServiceProvider:
$this->app->singleton(Liform::class, function ($app) {
return Liform::create();
});
Then inject Liform into controllers/services.
Form Type Extensions (2.0+)
Extend Liform's behavior for custom form types by implementing Limenius\Liform\Builder\FormTypeBuilderInterface:
use Limenius\Liform\Builder\FormTypeBuilderInterface;
class CustomTypeBuilder implements FormTypeBuilderInterface
{
public function build(array $config, FormTypeInterface $type, FormBuilderInterface $builder)
{
return [
'type' => 'string',
'custom_property' => $config['custom_value'] ?? null,
];
}
}
Register it in Liform's builder stack:
Liform::create()
->addBuilder(new CustomTypeBuilder())
->fromForm($form);
Liform to auto-generate request/response schemas for API endpoints:
$requestSchema = Liform::create()
->fromForm($this->formFactory->createNamedBuilder('UserCreate', null, null)->getForm())
->toArray();
$responseSchema = [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' 'string'],
],
];
isRequired() Return Value Changes (2.0.1)
AbstractTransformer::isRequired() now returns a boolean (previously may have returned mixed types).public function isRequired(): bool {
return $this->form->getConfig()->getOption('required');
}
Nested Forms and Circular References (2.0+)
setOption('max_depth', 3) or manually flatten schemas.Form Data vs. Schema Mismatch (2.0+)
null values for required fields).setOption('ignore_missing_data', false) or leverage isRequired() checks.Custom Form Types Not Recognized (2.0+)
Liform may not handle custom form types.FormTypeBuilderInterface or extend existing builders.JSON Schema Version Conflicts (2.0+)
Liform to use a specific version:
Liform::create()->setOption('json_schema_version', 'draft-07');
Inspect Intermediate Steps (Updated)
Use toJson() to debug raw output:
$schema = Liform::create()->fromForm($form);
dd($schema->toJson()); // Pretty-print JSON for inspection
Enable Verbose Logging (2.0+)
Liform::create()->setOption('verbose', true);
Logs transformations for each form field.
Check for Deprecated Methods (2.0+)
Liform updates for breaking changes (e.g., isRequired() return type in 2.0.1).Custom JSON Schema Modifiers (Updated)
Use setSchemaModifier() to post-process the schema:
Liform::create()
->fromForm($form)
->setSchemaModifier(function (array $schema) {
$schema['additionalProperties'] = false;
return $schema;
})
->toArray();
Dynamic Field Mapping (2.0+) Override field names or types dynamically:
Liform::create()
->fromForm($form)
->addFieldTransformer('email', function ($field) {
$field['format'] = 'email-address';
return $field;
});
Integration with Laravel Validation (2.0+)
Sync Liform schemas with Laravel's validation rules:
$rules = $form->getConfig()->getOption('validation_groups');
$schema = Liform::create()
->fromForm($form)
->setOption('validation_rules', $rules)
->toArray();
Localization Support (2.0+) Extract translations for form labels/placeholders:
$translator = app('translator');
$schema = Liform::create()
->fromForm($form)
->setTranslator($translator)
->toArray();
How can I help you explore Laravel packages today?