You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #1702 [LiveComponent] Document how to use extra_options in the Ajax-powered autocomplete (jakubtobiasz)
This PR was merged into the 2.x branch.
Discussion
----------
[LiveComponent] Document how to use `extra_options` in the Ajax-powered autocomplete
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | n/a
| Issues | requested in #1322 (comment)
| License | MIT
> Time spent on this PR has been sponsored by [Commerce Weavers](https://commerceweavers.com/) ♥️.
Commits
-------
aab8892 [LiveComponent] Document how to use `extra_options` in the Ajax-powered autocomplete
The ability to pass extra options was added in Autocomplete 2.14.
289
+
290
+
Autocomplete field options are not preserved when the field is rendered on an Ajax call. So, features like exclude some options
291
+
based on the current form data are not possible by default. To partially avoid this limitation, the `extra_options` option was added.
292
+
293
+
::warning
294
+
295
+
Only scalar values (`string`, `integer`, `float`, `boolean`), `null` and `arrays` (consisted from the same types as mentioned before) can be passed as extra options.
296
+
297
+
Considering the following example, when the form type is rendered for the first time, it will use the `query_builder` defined
298
+
while adding a `food` field to the `FoodForm`. However, when the Ajax is used to fetch the results, on the consequent renders,
299
+
the default `query_builder` will be used::
300
+
301
+
// src/Form/FoodForm.php
302
+
// ...
303
+
304
+
class FoodForm extends AbstractType
305
+
{
306
+
public function buildForm(FormBuilderInterface $builder, array $options): void
307
+
{
308
+
$currentFoodId = $builder->getData()->getId();
309
+
310
+
$builder
311
+
->add('food', FoodAutocompleteField::class, [
312
+
'query_builder' => function (EntityRepository $er) {
0 commit comments