How to use Autocomplete Form Types
Sylius provides ready-to-use autocomplete form types for selecting entities in the Admin panel. These types are built on top of Symfony UX Autocomplete and offer seamless integration with translatable Sylius resources.
Available Autocomplete Types
Sylius ships with the following autocomplete types:
ProductAutocompleteType
Product
ProductVariantAutocompleteType
ProductVariant
ProductAttributeAutocompleteType
ProductAttribute
TaxonAutocompleteType
Taxon
Using Built-in Autocomplete Types
To use a built-in autocomplete type in your form:
<?php
declare(strict_types=1);
namespace App\Form\Type;
use Sylius\Bundle\AdminBundle\Form\Type\ProductAutocompleteType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
final class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('product', ProductAutocompleteType::class, [
'label' => 'app.ui.select_product',
])
;
}
}Selecting Multiple Entities
Enable multiple selection with the multiple option:
Customizing via extra_options
All built-in autocomplete types support customization via extra_options. These options are passed through the AJAX request to the autocomplete endpoint.
choice_label
Entity property to display as label
name (or fullname for Taxon)
choice_value
Entity property to use as value
entity ID
entity_fields
Non-translated fields to search in
['code']
translation_fields
Translated fields to search in
['name']
Changing the Display Label
Customizing Search Fields
Limitations of extra_options
The extra_options array is serialized and passed via URL to the autocomplete endpoint. This means only scalar values and arrays are supported.
If you need advanced customization like custom query filtering (filter_query), you must create your own autocomplete type.
Creating a Custom Autocomplete Type
For Translatable Entities
If your entity has translations (implements TranslatableInterface), extend TranslatableAutocompleteType:
For Non-Translatable Entities
For entities without translations, extend BaseEntityAutocompleteType from Symfony UX:
Note: If you don't use autowiring and autoconfiguration, register your form type manually:
Last updated
Was this helpful?
