ux icon indicating copy to clipboard operation
ux copied to clipboard

[AutoComplete] Entity Autocomplete & Add If Not Exist

Open disceney opened this issue 1 year ago • 2 comments

Hello,

I am encountering a specific problem with Symfony UX Autocomplete.

Here's the issue:

I have an EntityType field with the attribute "autocomplete" => true. This allows me to search for an element from another table to establish a relation, and it works correctly. For example:

$formBuilder->add("fieldManyToMany", EntityType::class, [
    "required" => false,
    "multiple" => true,
    "class" => Entity::class,
    "choice_label" => "name",
    "autocomplete" => true
]);

However, this only displays the elements that already exist in the database. I would like to know if it is possible to use the Symfony UX Component to combine this functionality with the ability to add a new element via a DataTransformer.

The ideal solution would be to have:

  • A list of existing elements, while keeping the AJAX search system provided by the default plugin.
  • The ability to add a new element to the list, which would then be saved in the database upon submission.

If a solution or a similar issue has already been resolved, I would be interested, as I have tried multiple times without success. I still haven't been able to add a new element to my select. The system does not allow me to add new elements.

Thank you :)

disceney avatar Jun 06 '24 16:06 disceney

Would that looks like this ?

https://ux.symfony.com/demos/live-component/product-form

smnandre avatar Jun 06 '24 17:06 smnandre

Yes, that's roughly the idea. However, instead of opening a modal to add elements, it would be better to do it directly in the autocomplete select field

disceney avatar Jun 07 '24 10:06 disceney

Hey @disceney ! Saw you closed this issue. Did you find any solutions? I am also looking to build an autocomplete like this.

nina-alin avatar Oct 17 '24 13:10 nina-alin

@nina-alin This solution worked well form me : https://florentdestremau.bearblog.dev/using-symfony-autocomplete-to-create-entities-on-the-fly-in-a-form/

antoine1003 avatar Feb 02 '25 20:02 antoine1003

Yeah I ended up following this documentation too. Works pretty well 👍

nina-alin avatar Feb 25 '25 15:02 nina-alin