Skip to content

Commit 1e01865

Browse files
committed
minor #2297 [Autocomplete] Adjust index.rst format (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [Autocomplete] Adjust index.rst format Enforce max line-width Fix some minor code examples Commits ------- ea83070 [Autocomplete] Adjust index.rst format
2 parents 468dc67 + ea83070 commit 1e01865

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/Autocomplete/doc/index.rst

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Tom Select-powered UI control by adding the ``autocomplete`` option:
6868
}
6969
7070
That's all you need! When you refresh, the Autocomplete Stimulus controller
71-
will transform your select element into a smart UI control:
71+
will transform your ``<select>`` element into a smart UI control:
7272

7373
.. image:: food-non-ajax.png
7474
:alt: Screenshot of a Food select with Tom Select
@@ -99,7 +99,8 @@ Or, create the field by hand::
9999
// src/Form/FoodAutocompleteField.php
100100
// ...
101101

102-
use Symfony\Bundle\SecurityBundle\Security;
102+
use Symfony\Component\Form\AbstractType;
103+
use Symfony\Component\OptionsResolver\OptionsResolver;
103104
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
104105
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
105106

@@ -283,16 +284,22 @@ Passing Extra Options to the Ajax-powered Autocomplete
283284

284285
The ability to pass extra options was added in Autocomplete 2.14.
285286

286-
Autocomplete field options are not preserved when the field is rendered on an Ajax call. So, features like exclude some options
287-
based on the current form data are not possible by default. To partially avoid this limitation, the `extra_options` option was added.
287+
Autocomplete field options are **not preserved** when the field is rendered
288+
on an Ajax call. So, features like exclude some options based on the current
289+
form data are not possible by default.
290+
291+
To partially avoid this limitation, the ``extra_options`` option was added.
288292

289293
.. warning::
290294

291-
Only scalar values (``string``, ``integer``, ``float``, ``boolean``), ``null`` and ``array`` (consisted from the same types as mentioned before) can be passed as extra options.
295+
Only scalar values (``string``, ``integer``, ``float``, ``boolean``),
296+
``null`` and ``array`` (consisted from the same types as mentioned before)
297+
can be passed as extra options.
292298

293-
Considering the following example, when the form type is rendered for the first time, it will use the ``query_builder`` defined
294-
while adding a ``food`` field to the ``FoodForm``. However, when the Ajax is used to fetch the results, on the consequent renders,
295-
the default ``query_builder`` will be used::
299+
Considering the following example, when the form type is rendered for the first
300+
time, it will use the ``query_builder`` defined while adding a ``food`` field
301+
to the ``FoodForm``. However, when the Ajax is used to fetch the results, on
302+
the consequent renders, the default ``query_builder`` will be used::
296303

297304
// src/Form/FoodForm.php
298305
// ...
@@ -306,21 +313,18 @@ the default ``query_builder`` will be used::
306313
$builder
307314
->add('food', FoodAutocompleteField::class, [
308315
'query_builder' => function (EntityRepository $er) {
309-
$qb = $er->createQueryBuilder('o');
310-
311-
$qb->andWhere($qb->expr()->notIn('o.id', [$currentFoodId]));
312-
313-
return $qb;
314-
};
315-
}
316+
return $er->createQueryBuilder('o')
317+
->andWhere($qb->expr()->notIn('o.id', [$currentFoodId]));
318+
};
316319
])
317320
;
318321
}
319322
}
320323

321-
If some food can be consisted of other foods, we might want to exclude the "root" food from the list of available foods.
322-
To achieve this, we can remove the ``query_builder`` option from the above example and pass the ``excluded_foods`` extra option
323-
to the ``FoodAutocompleteField``::
324+
If some food can be consisted of other foods, we might want to exclude the
325+
"root" food from the list of available foods. To achieve this, we can remove
326+
the ``query_builder`` option from the above example and pass the ``excluded_foods``
327+
extra option to the ``FoodAutocompleteField``::
324328

325329
// src/Form/FoodForm.php
326330
// ...
@@ -341,13 +345,13 @@ to the ``FoodAutocompleteField``::
341345
}
342346
}
343347

344-
The magic of the ``extra_options`` is that it will be passed to the ``FoodAutocompleteField`` every time an Ajax call is made.
345-
So now, we can just use the ``excluded_foods`` extra option in the default ``query_builder`` of the ``FoodAutocompleteField``::
348+
The magic of the ``extra_options`` is that it will be passed to the ``FoodAutocompleteField``
349+
every time an Ajax call is made. So now, we can just use the ``excluded_foods``
350+
extra option in the default ``query_builder`` of the ``FoodAutocompleteField``::
346351

347352
// src/Form/FoodAutocompleteField.php
348353
// ...
349354

350-
use Symfony\Bundle\SecurityBundle\Security;
351355
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
352356
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
353357

@@ -593,12 +597,14 @@ Passing Extra Options to the Autocompleter
593597

594598
The ability to pass extra options was added in Autocomplete 2.14.
595599

596-
If you need to pass extra options to the autocompleter, you can do so by implementing the
597-
``\Symfony\UX\Autocomplete\OptionsAwareEntityAutocompleterInterface`` interface.
600+
If you need to pass extra options to the autocompleter, you can do so by
601+
implementing the ``\Symfony\UX\Autocomplete\OptionsAwareEntityAutocompleterInterface``
602+
interface.
598603

599604
.. tip::
600605

601-
If you want to know **why** you might need to use the ``extra_options`` feature, see :ref:`passing-extra-options-to-the-ajax-powered-autocomplete`.
606+
If you want to know **why** you might need to use the ``extra_options``
607+
feature, see :ref:`passing-extra-options-to-the-ajax-powered-autocomplete`.
602608

603609
.. code-block:: diff
604610
@@ -634,13 +640,13 @@ If you need to pass extra options to the autocompleter, you can do so by impleme
634640
+ return $qb;
635641
+ }
636642
637-
+/**
638-
+ * @param array<string, mixed> $options
639-
+ */
640-
+public function setOptions(array $options): void
641-
+{
642-
+ $this->options = $options;
643-
+}
643+
+ /**
644+
+ * @param array<string, mixed> $options
645+
+ */
646+
+ public function setOptions(array $options): void
647+
+ {
648+
+ $this->options = $options;
649+
+ }
644650
645651
.. _manual-stimulus-controller:
646652

0 commit comments

Comments
 (0)