I have a custom autocomplete input that is not bound to any entities:
$builder
->add('input', TextType::class, [
'autocomplete' => true,
'autocomplete_url' => 'https://path-to-autocomplete',
'tom_select_options' => [
'create' => false,
'preload' => true,
'maxItems' => 1,
'delimiter' => '/',
],
])
;
The input correctly requests the autocomplete URL, fetches results, renders the correct item label, and sends the correct item value with the form.
The problem arises after submitting the form at step #6.
- Empty form is rendered.
- We select an item with ID
15
and labelFoo
. - Input is rendered correctly.
- Form is submitted.
- Value of
15
is sent to the server with the form. - Now we have to re-render a form again with an initial value of the input of
15
.
At this point, the input value is rendered as 15
instead of Foo
. That makes perfect sense. The input just doesn't know how to get a label for an item with ID 15
.
Question: how do I provide the input with data about the item label?
I expected the it to have something like reverse_autocomplete_url
that would be called after input initialization to get items by their IDs but I don't think there is such an option.
You have to initialize the form field with options array:
value
andtext
keys are customizable viavalueField
andlabelField
properties.