I would like as for help. I have a form with dropdown list and I need to modify choices based on external input. I guess it should work well with eventListener
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use($input){
$form = $event->getForm();
// get existin form child
// modify list of choices
}
All samples I have seen are using FormEvents only to add new field, but I need to modify existing field but I don't know how to access it.
thanks for help
While the original question is rather old, let me leave this here in case someone else comes across the need of altering a specific option of a field without having to replicate all options again:
Source: https://github.com/symfony/symfony/issues/8513#issuecomment-21868035
What you can do is override the original child.
It worked for me.
NOTE: this will only work in PHP 5.4, as
$this
in a Closure is not available in PHP 5.3.There is a blog post here that works through an entire dynamic form for an entity relationship: http://aulatic.16mb.com/wordpress/2011/08/symfony2-dynamic-forms-an-event-driven-approach/
The Symfony site has this mostly documented too, you just need to write the ajax code and corresponding controller method which is done in the blog post above: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html