Hello, i am having a trouble with passing some options to the form fields in Symfony 5 Form Builder.
In the controller the form is being created:
$form = $this->createForm(FooFormType::class, $foo);
In the FooFormType.php
my buildForm() method looks like this:
$builder
->add('collection', FooAutocompleteField::class, [
'searchable_fields' => ['name'],
])
;
in the FooAutocompleteField.php
i have only getParent() that returns the Symfony UX ParentEntityAutocompleteType::class
and a configureOptions() which looks like this:
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Foo::class,
'searchable_fields' => ['override_me'],
]);
}
I need the searchable_fields
to be replace within the FooFormType.php
, ideally by passing an option in controller like this:
$form = $this->createForm(FooFormType::class, $foo,[
'searchable_fields' => ['name'],
]);
$resolver->setDefined('searchable_fields');
after ConfigureOptions of FooAutocompleteField.php
FooAutocompleteField.php
and the $options does indeed change, but the defaults in configureOptions() are not overriden.It seems that when i do this (add an attr option):
$builder
->add('collection', FooAutocompleteField::class, [
'searchable_fields' => ['name'],
'attr' = [
'class' => 'bg-primary',
'placeholder' => 'THIS IS OVERRIDING',
]
])
;
The options are in fact overriding and getting passed to FooAutocompleteField
!
But why not the 'searchable_fields'?
It seems like the initial form select options are overwritten, but the Ajax calls themselve use the Default configuration. Im stuck...
This is a known issue of symfony/ux: (Currently not really fixable.)
Here is the issue on github: https://github.com/symfony/ux/issues/420
You really cannot pass/override ANY options when you use an Ajax auto-complete field. This is because, on the Ajax call, we would have no way to re-create your AnyForm form (nor are we even aware of it). We simply re-create the FoodAutocompleteField and then use its query_builder.
Source : Ryan Weaver on issue