symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
856 stars 315 forks source link

[Autocomplete] (Searchable Fields): Incorrect search field selection due to similar field names #2370

Open HugoSEIGLE opened 1 week ago

HugoSEIGLE commented 1 week ago

Description:

In the MyAutocompleteField implementation, the searchable_fields option includes translations.name and foo.translations.name. However, when both fields share the same name (name), the autocomplete search only uses the first occurrence (translations.name). If the order is reversed, the search instead targets foo.translations.name, which leads to inconsistent search results depending on the field order.

This behavior impacts the usability of the autocomplete functionality, as it prevents the search from working properly across both related entities.

Steps to reproduce:

Define searchable_fields as ['translations.name', 'foo.translations.name'].
Trigger the autocomplete field search.
Notice that only translations.name is used in the query.
Reverse the field order to ['foo.translations.name', 'translations.name'].
Now, only foo.translations.name is considered in the search.

Expected behavior:

The autocomplete should search across both translations.name and foo.translations.name fields, regardless of their order in searchable_fields.

Actual behavior:

The search query only considers the first occurrence of name in searchable_fields, ignoring the second field. Proposed Solution:

Consider updating the query logic to handle multiple fields with the same name explicitly. This could involve aliasing the fields or extending the searchable_fields handling to ensure both translations.name and foo.translations.name are included in the search query.

Environment:

Symfony UX Autocomplete
Symfony Form Component
PHP 8.3

Code :


namespace App\Form\AutocompleteField\MyAutocomplete;

use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

use function sprintf;

#[AsEntityAutocompleteField(route: 'ux_entity_autocomplete')]
final class MyAutocompleteField extends AbstractType
{
    #[Override]
    public function configureOptions(OptionsResolver $optionsResolver): void
    {
        $optionsResolver
            ->setDefaults([
                'class' => MyEntity::class,
                'placeholder' => 'Choices',
                'searchable_fields' => ['translations.name', 'foo.translations.name'],
                'choice_label' => ChoiceList::label($this, $this->getChoiceLabel(...)),
                'query_builder' => ...,
            ]);
    }

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

    public function getChoiceLabel(): string
    {
        return ...;
    }
}
smnandre commented 1 week ago

Hi @HugoSEIGLE! Thank you for this report... with such a vision on the problem and where it occurs... would you maybe have time to submit a fix ? :)

HugoSEIGLE commented 1 week ago

Hi @HugoSEIGLE! Thank you for this report... with such a vision on the problem and where it occurs... would you maybe have time to submit a fix ? :)

Hi, it's done :)

smnandre commented 1 week ago

Well.. thank you very much! 😄

(i will look at it after the week-end)