Open
Description
I recently switched from regular "EntityType" form fields to "BaseEntityAutocompleteType", and most features work as expected, except for preferred choices, which appear to be totally ignored.
Given this form + this field :
<?php
namespace App\Form;
use App\Entity\Individual;
use App\Repository\BookmarkRepository;
use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
#[AsEntityAutocompleteField]
class AutocompleteTestType extends AbstractType
{
public function __construct(
private readonly BookmarkRepository $bookmarkRepository,
) {
}
#[Override]
public function buildForm(
FormBuilderInterface $builder,
array $options,
): void {
$builder
->setMethod('GET')
->add(
'test',
AutocompleteTestField::class,
[
'preferred_choices' => $this->getPreferredChoices(),
],
)
->add(
'submit',
SubmitType::class,
)
;
}
/**
* @return Individual[]
*/
private function getPreferredChoices(): iterable
{
$individuals = [];
foreach ($this->bookmarkRepository->findAll() as $bookmark) {
$individuals[] = $bookmark->getIndividual();
}
return $individuals;
}
}
<?php
namespace App\Form;
use App\Entity\Individual;
use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
#[AsEntityAutocompleteField]
class AutocompleteTestField extends AbstractType
{
#[Override]
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'autocomplete' => true,
'choice_label' => static fn(Individual $individual) => "{$individual->getLastName()}, {$individual->getFirstName()}",
'choice_value' => 'id',
'class' => Individual::class,
'multiple' => false,
]
);
}
#[Override]
public function getParent(): string
{
return BaseEntityAutocompleteType::class;
}
}
Expected behavior
Having the preferred choices showing before the autocomplete-fetched choices, like for non-autocomplete fields (EntityType, ChoiceType, etc).
...Or having the documentation reflect the absence of support for this feature.
Actual behavior
Starting with an empty selection, when I click on the form drop-down, the list is empty, the spinner shows, then the list of all choices is displayed, instead of showing preferred choices first, then the other choices.
Activity
smnandre commentedon Oct 11, 2024
https://symfony.com/bundles/ux-autocomplete/current/index.html#passing-extra-options-to-the-ajax-powered-autocomplete
jmfeurprier commentedon Oct 11, 2024
Thanks for the documentation link.
I adapted the code to be using the extra_options, but the preferred choices are still ignored :
carsonbot commentedon Apr 12, 2025
Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?
jmfeurprier commentedon Apr 12, 2025
No resolution so far.