是否可以用通用表单进行表单收集?交响乐

问题描述 投票:0回答:1

所以我正在做一些练习,所以我有一个巨大的表格(10 个练习)和一系列较小的表格(小练习)。我的问题是练习可以是任何东西,可以是一个 TextType 或一个密码字段,甚至 10 个数字字段,你明白了。我到处都看到集合类型具有某些数据的entry_type,并且它总是相同的..我想要一些通用的东西。 抱歉,如果之前有人问过这个问题,谢谢<3

当前当我使用一种类型时它正在工作。示例:

class MultipleExercisesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('exercises', CollectionType::class, [
                'entry_type' =>  ActivePassiveType::class,
                'label' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            
        ]);
    }
}

class ActivePassiveType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('activePassive', ChoiceType::class, [
                'choices' => [
                    'Aktiva' => true,
                    'Pasiva' => false,
                ],
                'expanded' => true,
                'multiple' => false,
                'label' => false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}
php symfony symfony-forms
1个回答
0
投票

CollectionType 主要用于数据库中的 OneToMany 等关系。但我想我知道你想要实现什么。

您可以像这样组合多种表单类型,这样您就可以拥有任意数量的独特表单...

您的主要表单类型:

use App\Form\ExerciseOneFormType;
use App\Form\ExerciseTwoFormType;

class MultipleExercisesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('exerciseOne', ExerciseOneFormType::class)
            ->add('exerciseTwo', ExerciseTwoFormType::class)
            ->add('submit', SubmitType::class, array(
                'label' => 'Submit'
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null
        ));
    }
}

ExerciseOneForm类型:

class ExerciseOneFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('test1', CheckboxType::class, array(
                'label' => 'Testing',
                'required' => false
            ))
            ->add('test2', TextType::class, array(
                'label' => 'Testing',
            ))
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => YourEntity::class //or null
        ));
    }
}

ExerciseTwoForm类型:

class ExerciseTwoFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('anotherTest1', TextType::class, array(
                'label' => 'Testing',
            ))
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => YourEntity::class //or null
        ));
    }
}

您的控制器:

#[Route('/exercises', name: 'exercises')]
public function Exercises(Request $request): Response
{
    $exercisesForm = $this->createForm(MultipleExercisesType::class, null, array());

    $exercisesForm->handleRequest($request);

    if ($exercisesForm->isSubmitted() && $exercisesForm->isValid()) {
        ....
    }

    return $this->render('exercises.html.twig', array(
        'exercises_form' => $exercisesForm->createView()
    ));
}

// Render the form in your twig file using form(exercises_form) or however you would like too.
© www.soinside.com 2019 - 2024. All rights reserved.