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.
15
and label Foo
.15
is sent to the server with the form.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:
'tom_select_options' => [
'options' => [
[
'value' => 15,
'text' => 'Foo',
],
],
],
value
and text
keys are customizable via valueField
and labelField
properties.