New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LiveComponent] Document how to use extra_options
in the Ajax-powered autocomplete
#1702
base: 2.x
Are you sure you want to change the base?
[LiveComponent] Document how to use extra_options
in the Ajax-powered autocomplete
#1702
Conversation
jakubtobiasz
commented
Apr 9, 2024
•
edited
edited
Q | A |
---|---|
Bug fix? | no |
New feature? | n/a |
Issues | requested in #1322 (comment) |
License | MIT |
a06528b
to
f57c765
Compare
Thanks @jakubtobiasz ! I'll make some minor comments / suggestions tonight, but this is very welcome ! |
@smnandre I believe there're might be some places to be improved, as I'm not the best to write docs, especially if most of them have some kind of standarization :D. So I'm open for any comments 🙌🏻. |
@@ -171,6 +171,109 @@ After creating this class, use it in your form: | |||
|
|||
Congratulations! Your ``EntityType`` is now Ajax-powered! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just before there is a caution block concerning options ... maybe it could be updated / rewritten to push the extra option handling ?
@@ -171,6 +171,109 @@ After creating this class, use it in your form: | |||
|
|||
Congratulations! Your ``EntityType`` is now Ajax-powered! | |||
|
|||
.. _passing-extra-options-to-the-ajax-powered-autocomplete: | |||
|
|||
Passing Extra Options to the Ajax-powered Autocomplete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this block after the standard options.. wdyt ?
(especially if we rewrite the caution block to add a link to this section)
Passing Extra Options to the Ajax-powered Autocomplete | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionadded:: 2.13 | |
.. versionadded:: 2.14 |
|
||
.. versionadded:: 2.13 | ||
|
||
The ability to pass extra options was added in LiveComponents 2.13. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ability to pass extra options was added in LiveComponents 2.13. | |
The ability to pass extra options was added in Autocomplete 2.14. |
Passing Extra Options to the Autocompleter | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionadded:: 2.13 | |
.. versionadded:: 2.14 |
|
||
.. versionadded:: 2.13 | ||
|
||
The ability to pass extra options was added in LiveComponents 2.13. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ability to pass extra options was added in LiveComponents 2.13. | |
The ability to pass extra options was added in Autocomplete 2.14. |
@@ -274,6 +377,9 @@ to the options above, you can also pass: | |||
Set to ``focus`` to call the ``load`` function when control receives focus. | |||
Set to ``true`` to call the ``load`` upon control initialization (with an empty search). | |||
|
|||
``extra_options`` (default ``[]``) | |||
Allow you to pass extra options for Ajax-based autocomplete fields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a link to the related section ?
#[AsEntityAutocompleteField] | ||
class FoodAutocompleteField extends AbstractType | ||
{ | ||
public function configureOptions(OptionsResolver $resolver) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function configureOptions(OptionsResolver $resolver) | |
public function configureOptions(OptionsResolver $resolver): void |
'query_builder' => function (Options $options) { | ||
return function (EntityRepository $er) use ($options) { | ||
$qb = $er->createQueryBuilder('o'); | ||
|
||
$excludedFoods = $options['extra_options']['excluded_foods'] ?? []; | ||
if ([] !== $excludedFoods) { | ||
$qb->andWhere($qb->expr()->notIn('o.id', $excludedFoods)); | ||
} | ||
|
||
return $qb; | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this example because it's very relatable.
On the other side, i'm not sure we want to push that much the "query_builder" option, as it may be hard to grasp for newcomers
Just wondering if we could illustrate the extra_options with something "simpler" ...
.. if not this example is great :)
A more sensible point here: i'd really like to warn users those values will be "readable" from front... or are they encoded i don't remember what choice was made in the end ? |
By default, when you pass any options while adding a field to a form type, they will be lost | ||
when the autocomplete field is rendered on an Ajax call. To partially avoid this limitation, | ||
the `extra_options` option was added. It can only hold a scalar values, but it covers most use cases. | ||
|
||
Considering the following example, when the form type is rendered for the first time, it will use the `query_builder` defined | ||
while adding a `food` field to the `FoodForm`. However, when the Ajax is used to fetch the results, on the consequent renders, | ||
the default `query_builder` will be used:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a personal feeling (and my english is quite limited so take this with a pinch of salt :) )
And i think @javiereguiluz vision would be much usefull than mine there :)
What i feel here is that those two paragraphs may be written with a tone more "neutral / factual" (not sure of the term)
Something like
Autocomplete field options are not kept from a request to another, so following renders ...
To persists options betwen [..], you must use extra_options [..]
::warning
only scalar (string, booleans ....)
And then the example.
not sure if what i'm saying is clear 👼
} | ||
} | ||
|
||
The magic of the `extra_options` option is that it will be passed to the `FoodAutocompleteField` every time an Ajax call is made. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic of the `extra_options` option is that it will be passed to the `FoodAutocompleteField` every time an Ajax call is made. | |
The magic of the `extra_options` is that it will be passed to the `FoodAutocompleteField` every time an Ajax call is made. |
@jakubtobiasz Thanks for this PR. In my use case, we need to overwrite the
|