Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FormFlow and listeners: form events seems not working, how to create dynamic forms? #63131

@gremo

Description

@gremo

Symfony version(s) affected

7.4

Description

I’m experiencing issues with the new step-based forms. When using “nested” forms, events don’t seem to be triggered, and as a result it’s impossible to create fields dynamically (as described in the guide). See "How to reproduce".

In addition, even when trying to call addEventListener on FormFlowBuilderInterface to dynamically create a field... it is absolutely unclear how this can be done in order to add a dynamic field to a specific step (and, in that case, does the step need to exist as empty beforehand?).

class TestFlow extends AbstractFlowType
{
    public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void
    {
        $builder
            ->addStep('step1', Step1Type::class, [
                'inherit_data' => true,
            ])
        ;

        $builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event) {
            // Now... what? How do I add a field to a specific step?
        });
    }
}

How to reproduce

class TestFlow extends AbstractFlowType
{
    public function buildFormFlow(FormFlowBuilderInterface $builder, array $options): void
    {
        $builder
            ->addStep('step1', Step1Type::class, [
                'inherit_data' => true,
            ]);
    }
}
class Step1Type extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event) {
            // This will not be called
        });
    }
}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions