简体   繁体   English

根据 ChoiceType 上的选择动态加载 EntityType 字段

[英]Dynamically load EntityType field according to selection on ChoiceType

Hey so I'm trying to dynamically load my EntityType data after I selected a choice in my ChoiceType.嘿,所以我尝试在我的 ChoiceType 中选择一个选项后动态加载我的 EntityType 数据。 If both of those dropdowns have been selected, I want to show the rest of the form.如果这两个下拉菜单都被选中,我想显示表格的 rest。

Could someone help me out making this, or link me an example?有人可以帮我做这个,或者给我一个例子吗?

My code:我的代码:

<?php

namespace App\Form;

use App\Entity\CivilianCriminalRecords;
use App\Entity\FineTypes;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CivilianCriminalRecordsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('fineTypeCategory', ChoiceType::class, [
            'label' => "Categorie type overtreding",
            'choices'  => [
                'Category 0' => 0,
                'Category 1' => 1,
                'Category 2' => 2,
                'Category 3' => 3,
                'Category 4' => 4,
                'Category 5' => 5,
            ],
        ]);

        // Load when fineTypeCategory is selected.
        $builder->add('offenceType', EntityType::class, [
            'class' => FineTypes::class,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('ft')
                    ->where('ft.category = :categoryId')
                    ->setParameter('categoryId', '0'); // Shouldn't be 0 but the id filled in the fineTypeCategory field.
            },
            'choice_label' => 'label',
        ]);

        // Load when fineTypeCategory AND offenceType is selected
        $builder
            ->add('offence', TextareaType::class, ['label' => 'Omschrijving van de overtreding', 'attr' => ['rows' => '5']])
            ->add('statement', TextareaType::class, ['label' => 'Verklaring van de overtreder', 'attr' => ['rows' => '5']])
            ->add('jailed', CheckboxType::class, ['label' => 'Gevangenisstraf ontvangen?'])
            ->add('save', SubmitType::class, ['label' => 'Overtreding uitschrijven'])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => CivilianCriminalRecords::class,
        ]);
    }
}

Try this approach.试试这个方法。 It is not tested, but you should see the idea.它没有经过测试,但你应该看到这个想法。 I don't know how your controller looks like, but something like this I think:我不知道你的 controller 是什么样的,但我认为是这样的:

public function indexAction(Request $request): Response
{
    //Get your entity
    $records = $this->getDoctrine()->getRepository(CivilianCriminalRecords::class)->find($id);
    $options['show_offence_type'] = !empty($request->request->get('fineTypeCategory'));
    $options['show_more_params'] = !empty($request->request->get('offenceType'));
    $form = $this->createForm(CivilianCriminalRecordsType::class, $records, $options);
    $form->handleRequest($request);
    if(
        $request->request->has('offence') && //means that the third part was visible and filled in
        $form->isSubmitted() && $form->isValid()){
        //process the form
    }
    return $this->render('form.html.twig', [
        'form' => $form->createView(),
        'show_offence_type' => $options['show_offence_type'],
        'show_more_params' => $options['show_more_params']
    ]);
}

Then the FormType:然后是FormType:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder->add('fineTypeCategory', ChoiceType::class, [
        'label' => "Categorie type overtreding",
        'choices'  => [
            'Category 0' => 0,
            'Category 1' => 1,
            'Category 2' => 2,
            'Category 3' => 3,
            'Category 4' => 4,
            'Category 5' => 5,
        ],
    ]);
    if($options['show_offence_type']){
        // Load when fineTypeCategory is selected.
        $builder->add('offenceType', EntityType::class, [
            'class' => FineTypes::class,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('ft')
                    ->where('ft.category = :categoryId')
                    ->setParameter('categoryId', '0'); // Shouldn't be 0 but the id filled in the fineTypeCategory field.
            },
            'choice_label' => 'label',
        ]);
    }
    if($options['show_more_params']){
        // Load when fineTypeCategory AND offenceType is selected
        $builder
            ->add('offence', TextareaType::class, ['label' => 'Omschrijving van de overtreding', 'attr' => ['rows' => '5']])
            ->add('statement', TextareaType::class, ['label' => 'Verklaring van de overtreder', 'attr' => ['rows' => '5']])
            ->add('jailed', CheckboxType::class, ['label' => 'Gevangenisstraf ontvangen?']);
    }
    $builder->add('save', SubmitType::class, ['label' => 'Overtreding uitschrijven']);
}

public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefaults([
        'data_class' => CivilianCriminalRecords::class,
        'show_offence_type' => false,
        'show_more_params' => false
    ]);
}

And the template should be like this (only relevant part here):模板应该是这样的(这里只有相关部分):

{% if show_offence_type %}
    {{ form_row(form.offenceType) }}
{% endif %}
{% if show_more_params %}
    {{ form_row(form.offence) }}
    {{ form_row(form.statement) }}
    {{ form_row(form.jailed) }}
{% endif %}

The idea is to configure and render the input fields only when the form was submitted with the very fist selection.我们的想法是仅在使用第一个选择提交表单时配置和呈现输入字段。 After that, show the next selection and let the user send the form again.之后,显示下一个选择并让用户再次发送表单。 Then your show the rest of the form.然后你显示表格的 rest。 At the end, when all parts of the form were filled, you can process the form.最后,当表格的所有部分都填写完毕后,您就可以处理表格了。 Happy testing!祝测试愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM