## Getting Started
### Minimal Steps to First Use
1. **Installation**
```bash
composer require bghanem/generate-form-bundle
Ensure your project is Symfony 4.2+ (compatible with Symfony 5/6 via backward compatibility).
Enable the Bundle
Add to config/bundles.php:
return [
// ...
Bg\GenerateFormBundle\BgGenerateFormBundle::class => ['all' => true],
];
Register Routes
Add to config/routes.yaml:
app_genform:
resource: '@BgGenerateFormBundle/Controller/'
type: annotation
prefix: /genform
Database Setup
composer require symfony/orm-pack symfony/maker-bundle
.env with your database (e.g., DATABASE_URL=mysql://user:pass@localhost/db_genform).php bin/console doctrine:database:create
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Generate a Form
/genform in your browser to access the admin interface./genform/submit/{formId}) via debug:router:
php bin/console debug:router | grep genform
Form Definition
/genform/admin) to:
name="email", validation="email|required".options=["Admin","User"], default="User".Rendering Forms
{{ render(controller('BgGenerateFormBundle:Form:render', {'formId': 1})) }}
{{ render(controller('BgGenerateFormBundle:Form:render', {
'formId': 1,
'data': {'email': 'user@example.com'}
})) }}
Handling Submissions
/genform/submit/{formId}.FormSubmitListener:
// src/EventListener/CustomFormSubmitListener.php
namespace App\EventListener;
use Bg\GenerateFormBundle\Event\FormSubmitEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomFormSubmitListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'bg_genform.form_submit' => 'onFormSubmit',
];
}
public function onFormSubmit(FormSubmitEvent $event)
{
$data = $event->getData();
// Custom logic (e.g., save to DB, trigger email)
}
}
services.yaml:
services:
App\EventListener\CustomFormSubmitListener:
tags: ['kernel.event_subscriber']
Dynamic Forms
$formTemplate = $this->getDoctrine()
->getRepository(Bg\GenerateFormBundle\Entity\FormTemplate::class)
->find($formId);
use Bg\GenerateFormBundle\Form\Type\DynamicFormType;
$builder->add('dynamicForm', DynamicFormType::class, [
'form_id' => $formId,
'data' => $initialData,
]);
Database Schema Conflicts
FormTemplate or FormField entities, drop and recreate the database to avoid schema conflicts:
php bin/console doctrine:database:drop --force
php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate
--dry-run to preview migrations:
php bin/console doctrine:migrations:diff --dry-run
Route Conflicts
/genform. If you have existing routes (e.g., /genform for an API), rename the prefix in routes.yaml:
app_genform:
prefix: /dynamic_forms # Custom prefix
Validation Rules
@Assert\Callback) won’t work in the UI. Extend the FormField entity or use a custom field type:
// src/Form/Extension/CustomFieldTypeExtension.php
namespace App\Form\Extension;
use Bg\GenerateFormBundle\Form\Type\FormFieldType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
class CustomFieldTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(\Symfony\Component\Form\FormEvents::PRE_SET_DATA, function ($event) {
$data = $event->getData();
if ($data->getType() === 'custom') {
$event->getForm()->add('custom_rule', TextType::class);
}
});
}
public function getExtendedType(): string
{
return FormFieldType::class;
}
}
Performance with Large Forms
$qb = $this->createQueryBuilder('f')
->where('f.form = :form')
->setParameter('form', $formTemplate);
$fields = $qb->getQuery()->getResult();
$cache = $this->get('cache.app');
$formTemplate = $cache->get('form_'.$formId, function() use ($formId) {
return $this->getDoctrine()->getRepository(FormTemplate::class)->find($formId);
});
Translation Issues
translations/ directory is configured:
# config/packages/translation.yaml
framework:
translation:
paths: ['%kernel.project_dir%/translations']
form.field.{field_name}.label).Check Form Submission Data
public function onFormSubmit(FormSubmitEvent $event)
{
file_put_contents(
'var/log/genform_submission.log',
print_r($event->getData(), true)
);
}
Validate Form Field Types
text, email, select, etc.). For custom types (e.g., date), ensure the FormField entity’s type field matches Symfony’s form types:
// Extend the bundle's FormFieldType class
class ExtendedFormFieldType extends FormFieldType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'types' => array_merge(parent::getDefaultTypes(), ['date' => DateType::class]),
]);
}
}
Clear Cache After Changes
php bin/console cache:clear
FormFieldType to support new field types (e.g., rich text, file uploads):
// src/Form/Type/CustomFieldType.php
namespace App\Form\Type;
use Bg\GenerateFormBundle\Form\Type\FormFieldType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class CustomFieldType extends FormFieldType
{
protected function getTypeMap()
{
return array_merge(parent::getTypeMap(), [
'rich_text' => TextareaType::class,
]);
}
}
How can I help you explore Laravel packages today?