Installation:
composer require genemu/form-bundle
Ensure compatibility with your Symfony version (e.g., 2.1.* for Symfony 2.1/2.2).
Enable the Bundle:
Add to app/AppKernel.php:
new Genemu\Bundle\FormBundle\GenemuFormBundle(),
Initialize Assets:
php app/console assets:install web/
First Use Case: Use Select2 for an enhanced dropdown in a form:
// src/AppBundle/Form/Type/MyType.php
use Genemu\Bundle\FormBundle\Form\Type\Select2Type;
$builder->add('fieldName', Select2Type::class, [
'choices' => ['Option 1', 'Option 2'],
'placeholder' => 'Select an option',
]);
Ensure JavaScript/CSS assets are loaded in your template:
{{ form_widget(form) }}
{% block javascripts %}
{{ parent() }}
{{ asset('bundles/genemuform/js/select2.min.js') }}
{{ asset('bundles/genemuform/css/select2.css') }}
{% endblock %}
Select2 Integration:
$builder->add('tags', Select2Type::class, [
'multiple' => true,
'ajax' => [
'url' => '/api/tags',
'data' => ['id' => 'query'],
],
]);
{{ form_widget(form.tags) }} and include Select2 JS/CSS.Captcha Integration:
$builder->add('captcha', 'genemu_form_captcha_gd');
$builder->add('recaptcha', 'genemu_form_recaptcha', [
'public_key' => 'YOUR_PUBLIC_KEY',
'private_key' => 'YOUR_PRIVATE_KEY',
]);
TinyMCE Editor:
$builder->add('content', 'genemu_form_tinymce', [
'config' => [
'plugins' => 'link image',
'toolbar' => 'bold italic | link image',
],
]);
{{ asset('bundles/genemuform/js/tinymce/tinymce.min.js') }}
Asset Management:
Use assets:install for production and assets:debug for development.
Override asset paths in config.yml if needed:
genemu_form:
assets_path: '%kernel.root_dir%/../vendor/genemu/form-bundle/web'
Dynamic Forms:
Combine with Symfony’s FormEvent to conditionally add fields (e.g., show ReCaptcha only on registration).
Validation: Extend default validators (e.g., for ReCaptcha):
use Genemu\Bundle\FormBundle\Validator\Constraints\ReCaptcha;
$builder->add('recaptcha', null, [
'constraints' => [new ReCaptcha()],
]);
Asset Loading:
assets:install or missing JS/CSS in templates causes silent failures.web/bundles/genemuform/ and included in Twig.Select2 AJAX Dependencies:
[{ "id": 1, "text": "Option 1" }, ...]
TinyMCE Configuration:
'config' => json_decode(file_get_contents('path/to/custom_config.json'), true),
ReCaptcha Errors:
Symfony Version Mismatch:
2.1.* with Symfony 2.3+ may break due to deprecated APIs.composer.json.Form Rendering:
Use {{ dump(form.vars) }} in Twig to inspect form variables and options.
JavaScript Errors: Check the browser console for missing dependencies (e.g., jQuery for Select2/TinyMCE). Fix: Ensure jQuery is loaded before bundle assets.
Captcha Validation:
For GD Captcha, verify the captcha session key matches the submitted value:
$session->get('captcha', null) === $submittedValue
Custom Form Types:
Extend existing types (e.g., Select2Type) by overriding the buildView/buildForm methods:
class CustomSelect2Type extends Select2Type {
public function buildView(FormView $view, FormInterface $form, array $options) {
$view->vars['custom_option'] = 'value';
}
}
Asset Overrides:
Override bundle assets by copying files to web/bundles/genemuform/ and modifying them.
Event Subscribers: Listen to form events to dynamically alter fields:
// src/AppBundle/EventListener/FormListener.php
public function onPreSetData(FormEvent $event) {
$form = $event->getForm();
if ($event->getData()->isAdmin()) {
$form->add('admin_field', Select2Type::class);
}
}
How can I help you explore Laravel packages today?