Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

Create Symfony Form where fields appear and disappear based on choice

I'm trying to create a form where some of the fields appear or disappear based on the choice the user make inside the first field.

Form Type

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('type', ChoiceType::class, [
                'placeholder' => 'Please select the type of difference',
                'choices'  => array(
                    'Patch' => 'Patch',
                    'Regional' => 'Regional',
                    'Version' => 'Version',
                    'Platform' => 'Platform',
                ),
            ])
                    ->add('maingame', EntityType::class, [
                    'class' => MainGame::class,
                    'placeholder' => 'Please select a game',
                    'choice_label' => 'title',
                ])
                    ->add('gameversion', EntityType::class, [
                        'class' => GameVersion::class,
                        'placeholder' => 'Please select a Main Game first',
                        'choice_label' => 'title',
                    ])
                    ->add('platform', EntityType::class, [
                        'class' => Platform::class,
                        'choice_label' => 'title',
                    ])
                    ->add('text')
                    ->add('nation', EntityType::class, [
                        'class' => Nation::class,
                        'choice_label' => 'name',
                    ])
                    ->add('confirm')
                ;
    }

I can't manage to find the right way to do it. Can someone help?

question from:https://stackoverflow.com/questions/65831291/create-symfony-form-where-fields-appear-and-disappear-based-on-choice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. You can create one form for every choice. This is clear and fast solution. You can show links instead of choice list. User choose type by clicking on link and you can render your form based on this choice. One choice one method with route and form in controller.

  2. Ajax - Handle javascript onChange event after choice was selected and retrieve and render updated form. You can pass this choice while building your form as option.

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('maingame', EntityType::class, [
                'class' => MainGame::class,
                'placeholder' => 'Please select a game',
                'choice_label' => 'title',
            ])
        ->add('platform', EntityType::class, [
            'class' => Platform::class,
            'choice_label' => 'title',
        ])
        ->add('text')
        ->add('confirm');
    switch ($options['type']) {
        case 'Patch':
            $builder->add('gameversion', EntityType::class, [
                'class' => GameVersion::class,
                'placeholder' => 'Please select a Main Game first',
                'choice_label' => 'title',
            ])
            break;
        case 'Regional':
            $builder->add('nation', EntityType::class, [
                'class' => Nation::class,
                'choice_label' => 'name',
            ])
            break;
        // etc...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...