antona/form-collection-bundle
Install the Bundle
composer require antona/form-collection-bundle
(Symfony Flex auto-registers the bundle.)
Include Web Components
Add the JS/CSS to your base template (e.g., base.html.twig):
<script src="{{ asset('bundles/onlinqformcollection/onlinq-collection.js') }}"></script>
<link rel="stylesheet" href="{{ asset('bundles/onlinqformcollection/onlinq-collection.css') }}">
Enable a Form Theme
Configure Twig to use a theme (e.g., Bootstrap 5) in config/packages/twig.yaml:
twig:
form_themes:
- '@OnlinqFormCollection/bootstrap_5_collection_theme.html.twig'
First Use Case
Replace CollectionType with OnlinqCollectionType in a form:
use Onlinq\FormCollectionBundle\Form\OnlinqCollectionType;
$builder->add('tags', OnlinqCollectionType::class, [
'entry_type' => TextType::class,
'allow_add' => true,
'allow_delete' => true,
'allow_move' => true, // Unique to OnlinqCollectionType
]);
Dynamic Collections
Use OnlinqCollectionType for sortable, addable, and deletable collections with minimal JS:
$builder->add('steps', OnlinqCollectionType::class, [
'entry_type' => StepType::class,
'prototype' => true, // Renders a template for new entries
'allow_move' => true,
'min' => 1, // Enforce minimum items
]);
Theming Integration Extend existing themes by copying and modifying:
@OnlinqFormCollection/collection_theme.html.twig (base)@OnlinqFormCollection/bootstrap_5_collection_theme.html.twig (Bootstrap 5)
Override in your project’s templates/form/ directory.Stimulus Integration For custom behavior, extend the web component via Stimulus controllers:
// assets/controllers/collection_controller.js
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect() {
this.element.addEventListener('onlinq-collection-add', (e) => {
// Custom logic on add
});
}
}
Attach to forms:
{{ form_start(form, { attr: { 'data-controller': 'collection' } }) }}
Validation Feedback Leverage Twig’s built-in validation display with the theme:
{% for error in form.errors %}
<div class="error">{{ error.message }}</div>
{% endfor %}
symfony/ux-live-component for reactive forms.ApiPlatform for nested collection handling.OnlinqCollectionType for domain-specific logic:
class CustomCollectionType extends OnlinqCollectionType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(['custom_option' => true]);
}
}
Asset Loading
assets:install after composer require breaks JS/CSS.php bin/console assets:install and clear cache.Theme Conflicts
@OnlinqFormCollection/collection_theme with Bootstrap themes may cause styling issues.bootstrap_5_collection_theme).Prototype Rendering
prototype: true requires a prototype template in your theme.prototype_row.html.twig if needed.JavaScript Dependencies
lit and stimulus. Conflicts may arise with other JS bundles.webpack.config.js includes:
Encore
.addEntry('app', './assets/app.js')
.splitEntry(chunk => chunk.name === 'app' && ['onlinq-collection']);
onlinq-collection errors (e.g., missing dependencies).{{ dump(form.vars) }} to inspect collection structure.console.log('Collection event:', event.detail);
Custom Templates
Override Twig templates in templates/form/OnlinqFormCollection/:
collection_widget.html.twigprototype_row.html.twigConfiguration
Extend OnlinqCollectionType options via a compiler pass:
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition(OnlinqCollectionType::class);
$definition->addMethodCall('setCustomOption', ['value']);
}
Localization
Translate labels/buttons by extending the theme and using trans:
<button>{{ 'onlinq.add'|trans }}</button>
allow_move if not needed to reduce JS payload.aria-labels are set in custom themes for screen readers.Symfony\Panther to test dynamic collection interactions:
$crawler->executeScript("document.querySelector('onlinq-collection').addItem()");
How can I help you explore Laravel packages today?