ux icon indicating copy to clipboard operation
ux copied to clipboard

[AutoComplete] "preferred_choices" are ignored

Open jmfeurprier opened this issue 1 year ago • 6 comments

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.

image

jmfeurprier avatar Oct 10 '24 21:10 jmfeurprier

https://symfony.com/bundles/ux-autocomplete/current/index.html#passing-extra-options-to-the-ajax-powered-autocomplete

smnandre avatar Oct 11 '24 02:10 smnandre

Thanks for the documentation link.

I adapted the code to be using the extra_options, but the preferred choices are still ignored :

<?php

namespace App\Form;

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,
                [
                    'extra_options' => [
                        'preferred_choices' => $this->getPreferredChoices(),
                    ],
                ],
            )
            ->add(
                'submit',
                SubmitType::class,
            )
        ;
    }

    /**
     * @return string[]
     */
    private function getPreferredChoices(): iterable
    {
        $individuals = [];

        foreach ($this->bookmarkRepository->findAll() as $bookmark) {
            $individuals[] = $bookmark->getIndividual()->getId();
        }

        return $individuals;
    }
}
<?php

namespace App\Form;

use App\Entity\Individual;
use App\Repository\IndividualRepository;
use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

#[AsEntityAutocompleteField]
class AutocompleteTestField extends AbstractType
{
    public function __construct(
        private readonly IndividualRepository $individualRepository,
    ) {
    }

    #[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,
                'preferred_choices' => $this->getPreferredChoices(...),
            ]
        );
    }

    /**
     * @return Individual[]
     */
    private function getPreferredChoices(Options $options): iterable
    {
        $ids = $options['extra_options']['preferred_choices'] ?? [];

        if (empty($ids)) {
            return [];
        }

        return $this->individualRepository->findBy(
            [
                'id' => $ids,
            ]
        );
    }

    #[Override]
    public function getParent(): string
    {
        return BaseEntityAutocompleteType::class;
    }
}

jmfeurprier avatar Oct 11 '24 12:10 jmfeurprier

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Apr 12 '25 12:04 carsonbot

No resolution so far.

jmfeurprier avatar Apr 12 '25 12:04 jmfeurprier

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Oct 13 '25 12:10 carsonbot

No resolution so far.

jmfeurprier avatar Oct 13 '25 12:10 jmfeurprier