Install the Bundle Add via Composer:
composer require derafu/symfony-form-bundle
Enable in config/bundles.php:
Derafu\FormBundle\DerafuFormBundle::class => ['all' => true],
Basic Form Integration Register the form type in a controller or service:
use Derafu\FormBundle\Form\Type\DerafuFormType;
$builder->add('myForm', DerafuFormType::class, [
'config' => [
'schema' => 'path/to/schema.json', // JSON schema for form structure
'ui' => 'path/to/ui.json', // UI schema for customization
],
]);
First Use Case: Dynamic Forms Use the bundle to render a form from a JSON schema (e.g., for admin panels or user profiles):
{{ form_start(form.myForm) }}
{{ form_widget(form.myForm) }}
{{ form_end(form.myForm) }}
Define Schemas
Store form structure in schema.json (e.g., fields, validation rules) and UI customization in ui.json (e.g., labels, placeholders).
Example schema.json:
{
"type": "object",
"properties": {
"name": { "type": "string", "title": "Full Name" },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "email"]
}
Integrate with Symfony Forms Bind schemas dynamically in controllers or services:
$form = $this->createForm(DerafuFormType::class, $user, [
'config' => [
'schema' => $this->getParameter('kernel.project_dir').'/config/forms/user_schema.json',
],
]);
Handle Submissions Process data like a standard Symfony form:
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// Save $data to database
}
PRE_SET_DATA, POST_SUBMIT).Schema Validation
schema.json is valid JSON Schema (use JSON Schema Validator).UI Schema Conflicts
{{ form_widget }}) requires explicit template overrides.@DerafuFormBundle/Resources/views/ for bundle assets).Dependency Injection
derafu/form is installed. Add it explicitly if missing:
composer require derafu/form
DERAFU_FORM_DEBUG=true in .env to log schema/UI parsing errors.Custom Form Types
Extend DerafuFormType to add logic:
class CustomDerafuFormType extends DerafuFormType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'custom_option' => true,
]);
}
}
Override Templates
Copy templates from vendor/derafu/symfony-form-bundle/Resources/views/ to templates/bundles/derafu_form/ to customize rendering.
Event Subscribers
Listen to derafu.form.pre_build or derafu.form.post_build events to modify forms dynamically:
$eventDispatcher->addListener(DerafuFormEvents::PRE_BUILD, function (FormEvent $event) {
$event->setForm($event->getForm()->add('extra_field', TextType::class));
});
How can I help you explore Laravel packages today?