tetranz / select2entity-bundle

A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.
MIT License
218 stars 110 forks source link

How to change the value used from the AJAX response? #160

Open ChangePlaces opened 4 years ago

ChangePlaces commented 4 years ago

I see 'text' is required in the AJAX response to show. How can I change this to a different value from 'text', to 'foo'? Maybe it could be a property on the form type?

tetranz commented 4 years ago

The id and text names are a select2 requirement. https://select2.org/data-sources/formats

There is no way to change it for now but I think it would be possible to enhance the templating functionality to provide a simple means to use a different property name. I can't make any promises but I agree that it would probably be a useful feature.

ChangePlaces commented 4 years ago

with select2 v.3 you could override the selection method and return the property you want to use. in 3 it was formatSelection and formatResults iirc. It looks like they've been renamed to templateSelection and TemplateResults for 4.*.

https://stackoverflow.com/questions/23643409/in-select2-how-do-formatselection-and-formatresult-work