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.