Can't get EasyAdminAutocompleteType to work in a collection
I have a form type that utilises the EasyAdminAutocompleteType which gets rendered as a plain select instead of select2.
The "Artist" field should be autocomplete:
Here's the configuration:
# /app/config/config.yml
easy_admin:
entities:
Event:
class: ApiBundle\Entity\Event
form:
fields:
- property: 'performances'
type: 'collection'
by_reference: false
type_options:
entry_type: 'AppBundle\Form\PerformanceType'
by_reference: false
and the form type:
// /src/AppBundle/Form/PerformanceType.php
// ...
class PerformanceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('artist', EasyAdminAutocompleteType::class, ['class' => Artist::class]);
$builder->add('songs_description', TextareaType::class);
}
// ...
}
I tried fixing it listening for "easyadmin.collection.item-added" event and applying select2 manually on that select element but autocompletion doesn't fetch values in this case.
Used versions: EasyAdmin: 1.16.5 Symfony: 3.1.10
Same issue 🤓
@mikeelltt, @shinework could you show us the code where you're initialize select2 manually on that select?
Hi @yceruto, I used this code:
$('.field-collection').on('easyadmin.collection.item-added', function() {
var $parent = $(this);
var $field = $parent.find('.field-easyadmin_autocomplete');
if (!$field.length) {
return;
}
var $input = $field.find('select').select2({
theme: 'bootstrap'
});
});
You can use createAutoCompleteFields() function instead? this solve your problem?
$('.field-collection').on('easyadmin.collection.item-added', function() {
createAutoCompleteFields();
});
I know that it does not make the best approach, but we would change this function for better reusability/runtime initialization of dynamic autocomplete fields (i.e. createAutoCompleteFields($input)).
@yceruto Thanks it works ✅
Thanks @yceruto, works correctly.
https://github.com/javiereguiluz/EasyAdminBundle/pull/1536 improving this initialization...
@yceruto, sounds good! Thank you 😀