Installation:
composer require genemu/form-bundle:2.2.*
Ensure your composer.json targets Symfony 2.3+.
Enable the Bundle:
Add new Genemu\Bundle\FormBundle\GenemuFormBundle() to AppKernel::registerBundles().
Initialize Assets:
php app/console assets:install web/
First Form Integration:
Use the Select2Type in a form builder:
use Genemu\Bundle\FormBundle\Form\Type\Select2Type;
$builder->add('fieldName', Select2Type::class, [
'choices' => ['option1' => 'Label 1', 'option2' => 'Label 2'],
'multiple' => true, // Optional
]);
Verify Assets:
Check web/bundles/genemuform/ for loaded JS/CSS. For Select2, include its template:
{% form_theme form _self %}
Select2 Integration:
ajax_url option:
$builder->add('user', Select2Type::class, [
'ajax_url' => '/api/users',
'ajax_data' => ['role' => 'admin'],
]);
GenemuFormBundle:Select2:select2_widget.html.twig in your theme.Captcha Usage:
CaptchaValidator:
$builder->add('captcha', CaptchaGDType::class);
config.yml:
genemu_form:
recaptcha:
public_key: 'your_public_key'
private_key: 'your_private_key'
Use in forms:
$builder->add('recaptcha', ReCaptchaType::class);
TinyMCE Editor:
$builder->add('content', TinyMceType::class);
config option:
$builder->add('content', TinyMceType::class, [
'config' => [
'plugins' => 'code',
'toolbar' => 'bold italic code',
],
]);
Form Theming:
Resources/views/Form/:
{# app/Resources/Form/fields.html.twig #}
{% block select2_widget %}
{{ parent() }}
<script>$(document).ready(function() { /* Custom JS */ });</script>
{% endblock %}
{% javascripts '@GenemuFormBundle/Resources/public/js/*' %} in layouts to bundle JS.use Genemu\Bundle\FormBundle\Validator\Constraints\Captcha;
$builder->add('captcha', CaptchaGDType::class, [
'constraints' => [new Captcha()],
]);
{% block javascripts %}.Asset Paths:
assets:install.web/bundles/genemuform/ exists. Run assets:install --symlink in dev.Select2 Dependencies:
Uncaught TypeError: $(...).select2 is not a function.{% javascripts
'//code.jquery.com/jquery-3.4.1.min.js'
'@GenemuFormBundle/Resources/public/js/select2/select2.min.js'
%}
ReCaptcha Errors:
TinyMCE Conflicts:
id in config:
'config' => ['selector' => 'form_editor_' . uniqid()]
Symfony 3/4+ Compatibility:
GenemuFormBundle in config/bundles.php to force load:
return [
// ...
Genemu\Bundle\FormBundle\GenemuFormBundle::class => ['all' => true],
];
_profiler) to inspect rendered fields.APP_DEBUG=true) to catch asset/JS errors.ajax_url endpoints manually (e.g., /api/users?role=admin).Custom Form Types:
Extend existing types (e.g., Select2Type) by creating a new class:
use Genemu\Bundle\FormBundle\Form\Type\Select2Type;
class CustomSelect2Type extends Select2Type {
public function getParent() { return Select2Type::class; }
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(['custom_option' => true]);
}
}
Override Templates:
Copy Resources/views/ from the bundle to your theme (e.g., app/Resources/GenemuFormBundle/).
Event Listeners:
Subscribe to form events (e.g., PRE_SET_DATA) to modify behavior:
$form->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->add('field', Select2Type::class, ['multiple' => true]);
});
Configuration:
Override bundle defaults in config.yml:
genemu_form:
select2:
theme: 'bootstrap' # Default: 'default'
tinymce:
base_url: '//cdn.tiny.cloud/1/your_api_key/tinymce/5/'
How can I help you explore Laravel packages today?