Symfony2 - modify form field with eventListener

2020-07-08 06:41发布

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

标签: symfony
3条回答
▲ chillily
2楼-- · 2020-07-08 07:33

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:

<?php

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $form = $event->getForm();

    // Get configuration & options of specific field
    $config = $form->get('field_to_update')->getConfig();
    $options = $config->getOptions();

    $form->add(
        // Replace original field... 
        'field_to_update',
        $config->getType()->getName(),
        // while keeping the original options... 
        array_replace(
            $options, 
            [
                // replacing specific ones
                'required' => false,
            ]
        )
    );
});

Source: https://github.com/symfony/symfony/issues/8513#issuecomment-21868035

查看更多
时光不老,我们不散
3楼-- · 2020-07-08 07:41

What you can do is override the original child.

$builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use($input){
            $form = $event->getForm();

            $form->add($this->factory->createNamed('name_to_override', 'choice', null,
                 array("choices" => array("choice"=>"value"))
                ));

        }

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.

查看更多
相关推荐>>
4楼-- · 2020-07-08 07:45

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

查看更多
登录 后发表回答