Installation:
composer require aqarmap/thrace-form-bundle
Enable the bundle in config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 2/3):
// Symfony 4+
Thrace\FormBundle\ThraceFormBundle::class => ['all' => true],
Basic Usage: Import the form type in your controller or entity:
use Thrace\FormBundle\Form\Type\DatePickerType;
Register it in your form builder:
$builder->add('birthday', DatePickerType::class);
First Use Case: Add a jQueryUI-powered datepicker to a form field:
$builder->add('eventDate', DatePickerType::class, [
'widget' => 'single_text',
'html5' => false,
'attr' => ['class' => 'datepicker']
]);
Resources/doc/index.md: Official documentation with detailed usage examples.Form/Type/ directory: All available form types (e.g., DatePickerType.php, Select2Type.php).Resources/public/js/ and Resources/public/css/: Asset files for client-side behavior.Basic Field Integration: Replace standard Symfony form fields with Thrace equivalents:
// Before
$builder->add('title', TextType::class);
// After
$builder->add('title', InputLimiterType::class, [
'maxlength' => 100,
'error_bubbling' => true
]);
jQueryUI Widgets: Configure jQueryUI-powered fields with options:
$builder->add('budget', SliderType::class, [
'min' => 0,
'max' => 1000,
'step' => 50,
'options' => [
'range' => 'min',
'value' => 500
]
]);
Select2 Integration:
Enhance <select> fields with search, tags, and AJAX:
$builder->add('tags', Select2Type::class, [
'choices' => $tags,
'multiple' => true,
'placeholder' => 'Select tags...',
'ajax' => [
'url' => $this->generateUrl('api_tags'),
'data' => ['q' => 'term']
]
]);
TinyMCE Editor: Embed a WYSIWYG editor:
$builder->add('description', TinyMceType::class, [
'config' => [
'plugins' => 'link,image',
'toolbar' => 'bold,italic,link,image'
]
]);
Collection Fields: Dynamically add/remove nested forms (e.g., for one-to-many relationships):
$builder->add('items', CollectionType::class, [
'entry_type' => ItemType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true
]);
Dependent Fields: Chain Select2 fields where one field filters another:
$builder->add('country', Select2Type::class, ['choices' => $countries]);
$builder->add('city', Select2DependentType::class, [
'choices' => $cities,
'dependent_field' => 'country',
'ajax_url' => $this->generateUrl('api_cities')
]);
Asset Management:
Ensure jQuery and jQueryUI are loaded before Thrace assets. Use Symfony’s twig to include:
{{ encore_entry_link_tags('app') }}
<script src="{{ asset('bundles/thraceform/js/thrace-form.js') }}"></script>
Configuration:
Override default options in config/packages/thrace_form.yaml:
thrace_form:
datepicker:
date_format: 'dd/MM/yyyy'
autoclose: true
Validation: Combine with Symfony’s validators:
$builder->add('rating', RatingType::class, [
'min' => 1,
'max' => 5,
'constraints' => [
new NotBlank(),
new Range(['min' => 1, 'max' => 5])
]
]);
Doctrine Integration:
For jqgrid multi-select, ensure ThraceDataGridBundle is installed and configured:
$builder->add('roles', MultiSelectType::class, [
'grid_options' => [
'url' => $this->generateUrl('api_roles_grid')
]
]);
Asset Loading Order:
<!-- Correct order -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
{{ encore_entry_link_tags('app') }}
<script src="{{ asset('bundles/thraceform/js/thrace-form.js') }}"></script>
Symfony Version Mismatch:
ClassNotFoundException.composer.json requirements. For Symfony 4/5, use ~3.0 branch:
composer require aqarmap/thrace-form-bundle@dev-master
TinyMCE Configuration:
tinymce is installed via npm and built:
npm install tinymce
yarn encore dev
config option in TinyMceType includes valid plugins/toolbar.Select2 AJAX Dependencies:
[
{"id": 1, "text": "City 1"},
{"id": 2, "text": "City 2"}
]
ajax_url and data options in Select2DependentType.CollectionType Prototype:
prototype: true and the prototype template exists in your theme:
{# templates/collection_prototype.html.twig #}
<div class="collection-item">
{{ form_row(form) }}
<button type="button" class="remove-item">Remove</button>
</div>
Recaptcha Validation:
site_key and secret in RecaptchaType match your reCAPTCHA settings. Enable error bubbling:
$builder->add('recaptcha', RecaptchaType::class, [
'error_bubbling' => true
]);
Date Format Mismatch:
date_format in the type options or globally:
$builder->add('eventDate', DatePickerType::class, [
'date_format' => 'Y-m-d', // ISO format
'widget' => 'single_text'
]);
Console Errors:
$ is available globally.Form Dumping: Use Symfony’s form dumper to inspect field options:
$form->createView();
dump($form->createView()->children);
Event Listeners: Override field behavior with events:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
// Custom logic here
});
Twig Debugging: For Select2 or TinyMCE, inspect rendered HTML to ensure assets are correctly included:
{% if app.debug %}
{{ dump(form.vars) }}
{% endif %}
DatePickerType) by creating a custom class:How can I help you explore Laravel packages today?