ux icon indicating copy to clipboard operation
ux copied to clipboard

[Feature Request][Autocomplete] Add 'ajaxify' option

Open jerrac opened this issue 3 years ago • 1 comments

First, Autocomplete is really really nice. Thanks for making it. :)

On to my idea. It's really simple, but likely rather harder to implement than I might think...

Instead of creating an AutocompleteField type to enable ajaxifying your autocomplete, just add an 'ajax' => true option to your form element. Then Autocomplete will do the rest.

 $builder->add('my_amazing_field', EntityType::class, [
          'autocomplete' => true,
          'ajax' => true,
]);

Anyway, if my idea is possible, I'd love to see it implemented, if not, I'm curious to learn why.

Thanks for your time!

jerrac avatar Aug 18 '22 02:08 jerrac

Hi there!

This... MAY be possible, but indeed, it's difficult, which is why we currently require you to have a separate field. The tricky thing is that, in order for the Ajax endpoint to function correctly, that endpoint needs to know things like (A) which entity are we autocompleting, (B) how should I build the visual label for each option or even (C) what custom query_builder should I use?

To handle this, we force you to create your own form type which contains all of this info. Then the Ajax request simply contains a unique "identifier" that basically "points to this class". The endpoint an then use the field class for all the info it needs.

Adding an 'ajax' => true option IS possible... at least in some limited cases:

A) We could support this for simple fields by passing the (A) entity class or even (B) the choice label (as long as its not a callback) to the frontend and then include it as query parameters to the Ajax endpoint. Any more complex things (like a custom query builder) would not be allowed (you would need to do create the custom field like you do now). B) To avoid someone tampering with the query parameter (e.g. a bad user simply changes ?class=App\\Entity\\Foo to a different class to attempt to read any entity in your database that they want), the request would need to be signed. That's not a big deal - just something that would need to be done.

If someone wanted to contribute this, I would be open to it :).

Cheers!

weaverryan avatar Aug 23 '22 13:08 weaverryan

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

carsonbot avatar Apr 26 '24 12:04 carsonbot

It's still a good idea, but I can't work on it, and I'm guessing the same for the maintainers.

jerrac avatar Apr 26 '24 14:04 jerrac