{"slug":"ux-autocomplete","title":"ux-autocomplete","summary":"Symfony UX Autocomplete: searchable selects and Ajax suggestions for large Symfony form lists. Use for EntityType, ChoiceType, Tom Select, the autocomplete option, #[AsEntityAutocompleteField], #[AsAutocompleteField], AutocompleterInterface, extra_options and tom_select_options.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-02T16:48:37.23477Z","repo":{"url":"https://github.com/smnandre/symfony-ux-skills","stars":191,"forks":15,"license":"MIT","updatedAt":"2026-12-02T03:34:06Z"},"bodyHtml":"<hr>\n<h2>name: ux-autocomplete\ndescription: 'Symfony UX Autocomplete: searchable selects and Ajax suggestions for large Symfony form lists. Use for EntityType, ChoiceType, Tom Select, the autocomplete option, #[AsEntityAutocompleteField], #[AsAutocompleteField], AutocompleterInterface, extra_options and tom_select_options.'\nlicense: MIT\nmetadata:\nauthor: Simon André\nurl: <a href=\"https://smnandre.dev\">https://smnandre.dev</a></h2>\n<h1>UX Autocomplete</h1>\n<p>Turn any <code>ChoiceType</code>, <code>EntityType</code>, <code>EnumType</code>, <code>TextType</code> or plain <code>&lt;select&gt;</code> into a searchable control powered by Tom Select. Options are filtered in the browser, or loaded through Ajax from Doctrine or any other data source.</p>\n<h2>Installation</h2>\n<pre><code>composer require symfony/ux-autocomplete\n</code></pre>\n<p>Requires StimulusBundle. With Encore: <code>npm install --force</code>, then restart <code>npm run watch</code>.</p>\n<h2>Choose the Mode</h2>\n<table>\n<thead>\n<tr>\n<th>Situation</th>\n<th>Mode</th>\n<th>How</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>A few dozen options</td>\n<td>Local</td>\n<td><code>'autocomplete' =&gt; true</code> on the field</td>\n</tr>\n<tr>\n<td>Many Doctrine entities</td>\n<td>Ajax, entity</td>\n<td>Field class with <code>#[AsEntityAutocompleteField]</code>, parent <code>BaseEntityAutocompleteType</code></td>\n</tr>\n<tr>\n<td>API, search index, enum, array</td>\n<td>Ajax, any source</td>\n<td>Field class with <code>#[AsAutocompleteField]</code>, parent <code>AutocompleteChoiceType</code></td>\n</tr>\n<tr>\n<td>No Symfony Form</td>\n<td>Ajax endpoint</td>\n<td><code>AutocompleterInterface</code> service + Stimulus controller</td>\n</tr>\n<tr>\n<td>Free tags</td>\n<td>Create items</td>\n<td><code>TextType</code> with <code>'autocomplete' =&gt; true</code> and <code>create</code></td>\n</tr>\n</tbody>\n</table>\n<p>Since Symfony UX 3.5+, the package works with any data source: <code>#[AsAutocompleteField]</code>, <code>AutocompleteChoiceType</code>, <code>AutocompleterInterface</code>, the <code>ux_autocomplete</code> route and <code>max_options</code>.</p>\n<h2>Local (All Options in the Page)</h2>\n<pre><code>$builder\n    -&gt;add('food', EntityType::class, [\n        'class' =&gt; Food::class,\n        'placeholder' =&gt; 'What should we eat?',\n        'autocomplete' =&gt; true,\n    ])\n    -&gt;add('portionSize', ChoiceType::class, [\n        'choices' =&gt; ['small' =&gt; 's', 'medium' =&gt; 'm', 'large' =&gt; 'l'],\n        'autocomplete' =&gt; true,\n    ]);\n</code></pre>\n<h2>Ajax With Doctrine</h2>\n<pre><code>php bin/console make:autocomplete-field   # choose \"Entity (Doctrine)\"\n</code></pre>\n<pre><code>use Symfony\\Component\\Form\\AbstractType;\nuse Symfony\\Component\\OptionsResolver\\OptionsResolver;\nuse Symfony\\UX\\Autocomplete\\Form\\AsEntityAutocompleteField;\nuse Symfony\\UX\\Autocomplete\\Form\\BaseEntityAutocompleteType;\n\n#[AsEntityAutocompleteField]\nfinal class FoodAutocompleteField extends AbstractType\n{\n    public function configureOptions(OptionsResolver $resolver): void\n    {\n        $resolver-&gt;setDefaults([\n            'class' =&gt; Food::class,\n            'placeholder' =&gt; 'What should we eat?',\n            'searchable_fields' =&gt; ['name', 'category.name'],   // all fields when null\n            'security' =&gt; 'ROLE_USER',                          // endpoint is public otherwise\n        ]);\n    }\n\n    public function getParent(): string\n    {\n        return BaseEntityAutocompleteType::class;\n    }\n}\n</code></pre>\n<pre><code>$builder-&gt;add('food', FoodAutocompleteField::class);\n</code></pre>\n<h2>Ajax With Any Data Source</h2>\n<pre><code>php bin/console make:autocomplete-field   # choose \"Choice (any data source)\"\n</code></pre>\n<pre><code>use Symfony\\UX\\Autocomplete\\Form\\AsAutocompleteField;\nuse Symfony\\UX\\Autocomplete\\Form\\AutocompleteChoiceType;\n\n#[AsAutocompleteField]\nfinal class CountryAutocompleteField extends AbstractType\n{\n    public function configureOptions(OptionsResolver $resolver): void\n    {\n        $resolver-&gt;setDefaults([\n            'choices' =&gt; ['France' =&gt; 'FR', 'Germany' =&gt; 'DE', 'United Kingdom' =&gt; 'GB'],\n            'placeholder' =&gt; 'Choose a country',\n        ]);\n    }\n\n    public function getParent(): string\n    {\n        return AutocompleteChoiceType::class;\n    }\n}\n</code></pre>\n<p>Any <code>ChoiceType</code> option works; choices are filtered and paginated by the endpoint.</p>\n<h2>The Golden Rule: Options Live in the Field Class</h2>\n<p>The Ajax endpoint rebuilds the field from its class alone. Options passed to <code>-&gt;add()</code> exist on the first render only:</p>\n<pre><code>// WRONG: ignored during the Ajax search\n$builder-&gt;add('food', FoodAutocompleteField::class, [\n    'query_builder' =&gt; fn (EntityRepository $er) =&gt; $er-&gt;createQueryBuilder('f')-&gt;andWhere('f.id != :id'),\n]);\n\n// RIGHT: pass scalar data as extra_options, read it in the field class\n$builder-&gt;add('food', FoodAutocompleteField::class, [\n    'extra_options' =&gt; ['excluded_foods' =&gt; [$currentFood-&gt;getId()]],\n]);\n</code></pre>\n<pre><code>use Symfony\\Component\\OptionsResolver\\Options;\n\n$resolver-&gt;setDefaults([\n    'class' =&gt; Food::class,\n    'query_builder' =&gt; function (Options $options) {\n        return function (EntityRepository $er) use ($options) {\n            $qb = $er-&gt;createQueryBuilder('o');\n            $excluded = $options['extra_options']['excluded_foods'] ?? [];\n            if ([] !== $excluded) {\n                $qb-&gt;andWhere($qb-&gt;expr()-&gt;notIn('o.id', $excluded));\n            }\n\n            return $qb;\n        };\n    },\n]);\n</code></pre>\n<p><code>extra_options</code> only carries scalars, <code>null</code> and arrays of those. They travel in the URL, protected against tampering by a checksum.</p>\n<h2>Main Options</h2>\n<table>\n<thead>\n<tr>\n<th>Option</th>\n<th>Default</th>\n<th>Applies to</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>autocomplete</code></td>\n<td><code>false</code></td>\n<td>Any choice or text field</td>\n</tr>\n<tr>\n<td><code>tom_select_options</code></td>\n<td><code>[]</code></td>\n<td>Any; Tom Select settings, including <code>plugins</code></td>\n</tr>\n<tr>\n<td><code>options_as_html</code></td>\n<td><code>false</code></td>\n<td>Any; labels are escaped unless enabled</td>\n</tr>\n<tr>\n<td><code>max_options</code></td>\n<td><code>null</code> (50 for Ajax)</td>\n<td>Options displayed at once</td>\n</tr>\n<tr>\n<td><code>min_characters</code></td>\n<td><code>3</code></td>\n<td>Ajax fields</td>\n</tr>\n<tr>\n<td><code>max_results</code></td>\n<td><code>10</code></td>\n<td>Results per page returned by the endpoint</td>\n</tr>\n<tr>\n<td><code>preload</code></td>\n<td><code>focus</code></td>\n<td>Ajax fields: <code>focus</code>, <code>true</code> or <code>false</code></td>\n</tr>\n<tr>\n<td><code>searchable_fields</code></td>\n<td><code>null</code> (all)</td>\n<td>Entity fields; relations allowed (<code>category.name</code>)</td>\n</tr>\n<tr>\n<td><code>filter_query</code></td>\n<td><code>null</code></td>\n<td>Entity fields; full control of the search, replaces <code>searchable_fields</code> and <code>max_results</code></td>\n</tr>\n<tr>\n<td><code>security</code></td>\n<td><code>false</code></td>\n<td>Ajax fields: a role or a callable</td>\n</tr>\n<tr>\n<td><code>extra_options</code></td>\n<td><code>[]</code></td>\n<td>Ajax fields</td>\n</tr>\n<tr>\n<td><code>preferred_choices</code></td>\n<td></td>\n<td>Since 3.7: listed first in Ajax results; entities only for entity fields</td>\n</tr>\n</tbody>\n</table>\n<p>Texts (<code>loading_more_text</code>, <code>no_results_found_text</code>, <code>no_more_results_text</code>) are translated in the <code>AutocompleteBundle</code> domain. See <a href=\"references/api.md\">references/api.md</a> for the full reference, autocompleters, the JSON format and Stimulus values.</p>\n<h2>Styling</h2>\n<p>In <code>assets/controllers.json</code>, switch the Tom Select theme:</p>\n<pre><code>\"autoimport\": {\n    \"tom-select/dist/css/tom-select.default.css\": false,\n    \"tom-select/dist/css/tom-select.bootstrap5.css\": true\n}\n</code></pre>\n<h2>Key Principles</h2>\n<p><strong>Local first, Ajax when the list is large.</strong> Local mode works with the field option alone.</p>\n<p><strong>Secure every Ajax field that exposes private data.</strong> The endpoint is public by default; set <code>security</code>, or move it under a firewall with a custom route.</p>\n<p><strong>Configure in the class, vary with <code>extra_options</code>.</strong></p>\n<p><strong>Keep <code>options_as_html</code> off unless labels are trusted HTML.</strong> Labels are escaped to prevent XSS.</p>\n<h2>Gotchas</h2>\n<ul>\n<li><strong>Search ignores my <code>query_builder</code> / <code>choices</code> / <code>preferred_choices</code></strong>: they were passed to <code>-&gt;add()</code>. Move them into the field class or use <code>extra_options</code>.</li>\n<li><strong>Field class not found by the endpoint</strong>: the attribute is missing, or <code>getParent()</code> returns the wrong type.</li>\n<li><strong><code>filter_query</code> with <code>searchable_fields</code> or <code>max_results</code></strong>: incompatible, pick one.</li>\n<li><strong>Extending Tom Select from JavaScript does nothing</strong>: the extending controller must load eagerly (remove <code>stimulusFetch: 'lazy'</code>) to catch <code>autocomplete:pre-connect</code>.</li>\n<li><strong><code>ux_entity_autocomplete</code> deprecation</strong>: use the <code>ux_autocomplete</code> route.</li>\n<li><strong><code>TypeTestCase</code> fails on the <code>autocomplete</code> option</strong>: register <code>new AutocompleteChoiceTypeExtension()</code> in <code>getTypeExtensions()</code>.</li>\n</ul>\n<h2>References</h2>\n<ul>\n<li><a href=\"https://ux.symfony.com/autocomplete\">Symfony UX package page</a> (<a href=\"https://ux.symfony.com/autocomplete.md\">Markdown summary</a>) · <a href=\"https://packagist.org/packages/symfony/ux-autocomplete\"><code>symfony/ux-autocomplete</code> on Packagist</a>.</li>\n<li>Try the <a href=\"https://ux.symfony.com/autocomplete#demo\">interactive autocomplete demo</a>.</li>\n<li>See how <a href=\"https://ux.symfony.com/demos/live-component/dependent-form-fields\">autocomplete works with dependent LiveComponent fields</a>.</li>\n<li>For form fields, Ajax autocompleters and Tom Select integration, see the <a href=\"https://symfony.com/bundles/ux-autocomplete\">Symfony UX Autocomplete documentation</a>.</li>\n<li>For <code>tom_select_options</code> and JavaScript plugins, see the <a href=\"https://tom-select.js.org/docs/\">Tom Select documentation</a>.</li>\n</ul>\n<h2>See Also</h2>\n<ul>\n<li><strong>stimulus</strong>: extend Tom Select through <code>autocomplete:pre-connect</code> and <code>autocomplete:connect</code>.</li>\n<li><strong>live-component</strong>: autocomplete fields work inside <code>ComponentWithFormTrait</code> forms.</li>\n</ul>\n","files":[{"path":"references/api.md","sizeBytes":9700,"isText":true},{"path":"SKILL.md","sizeBytes":8514,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-02T16:49:15.942912Z","sha256":"DD868B321AA334AF003450A04A684356661E745415CCE13827A54B7C41CFCEA4","sizeBytes":6776},"review":null,"source":{"repositoryUrl":"https://github.com/smnandre/symfony-ux-skills","path":"skills/ux-autocomplete","license":"MIT","commit":"78c25ed0b79bcc21d7611002dc81d8b87c3316e2","subtreeSha":"6B25F0DB97D8D4A55E309D0644224573A6A434CD3485BA28A8EF4B5D0BB30DBD","lastSyncedAt":"2026-12-02T16:48:36.132977Z"},"reviewedAt":"2026-12-02T16:50:35.046404Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/smnandre/symfony-ux-skills/tree/main/skills/ux-autocomplete"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install smnandre-symfony-ux-skills@llmmart"},{"target":"git","command":"git clone https://github.com/smnandre/symfony-ux-skills.git"}]}