单击按钮复制表单

Duplicating form on button click

我有一个非常简单的表单,其中包含 2 个字段,我想要做的只是一个按钮,用户可以在其中复制此表单以批量添加多个条目。

What i would like to do (I can't integrate images yet, sry..)

我不确定如何集成它,我发现 some documentation 关于表单集合,但我不确定它是否符合我的需要,而且它适用于 Symfony 2,我正在使用最新版本 (Symfony 5)

我想我可以用 href='#?duplicateForm=1' 添加我的按钮,并用控制器和类型来处理它,但这对我来说似乎不是一个合适的解决方案......把用户输入扔进去是个坏主意for 语句。

我的类型class:

表单根据当前用户角色不同,管理员有更多字段。

class NetworkFolderAccessType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($options['type'] == 'ADMIN_FORM'){
            $builder
            ->add('userId', EntityType::class, [
                'class' => User::class,
                'choice_label' => function ($user) {
                    return $user->getNom() . ' ' . $user->getPrenom() .' ('. $user->getUsername().')';
                },
                'label'=>'Demandeur',
                'attr'=> [
                    'class'=>'form-control'
                ]
            ])
            ->add('validated',ChoiceType::class,[
                'expanded'=>false,
                'multiple'=>false,
                'choices'=>['Oui'=>true,'Non'=>false],
                'label'=>'Validé par le responsable',
                'attr'=> [
                    'class'=>'form-control'
                ],
                'choice_attr'=> [
                    'class'=>'checkbox-inline'
                ]
            ]);
        }
        if($options['type'] == 'USER_FORM' || $options['type'] == 'ADMIN_FORM'){
            $builder
            ->add('dataPath',TextType::class,[
                'attr'=> [
                    'class'=>'form-control'
                ],
                'label'=>'Chemin d\'accès'
            ])
            ->add('rights', ChoiceType::class, [
                'choices' => [
                    'Lecture' => 'read',
                    'Écriture' => 'write',
                    'Lecture & Écriture' => 'readwrite',
                ],
                'expanded' => false,
                'multiple' => false,
                'label'=>'Droits d\'accès',
                'attr' => ['class' => 'form-control'],
            ])
        ;
        } 
    }

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

我的控制器:

/**
     * @Route("/new", name="network_folder_access_new", methods={"GET","POST"})
     * @IsGranted("ROLE_USER")
     */
    public function new(Request $request): Response
    {
        $networkFolderAccess = new NetworkFolderAccess();

        if ($this->isGranted('ROLE_ADMIN'))
        $form = $this->createForm(NetworkFolderAccessType::class, $networkFolderAccess, ['type'=>'ADMIN_FORM']);
        elseif ($this->isGranted('ROLE_USER'))
        $form = $this->createForm(NetworkFolderAccessType::class, $networkFolderAccess, ['type'=>'USER_FORM']);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $networkFolderAccess->setDateDemand(new DateTime());
            if ($this->IsGranted('ROLE_USER') && !$this->isGranted('ROLE_ADMIN'))
            $networkFolderAccess->setUserId($this->security->getUser());

            $entityManager->persist($networkFolderAccess);
            $entityManager->flush();

            return $this->redirectToRoute('network_folder_access_index');
        }

        return $this->render('network_folder_access/new.html.twig', [
            'network_folder_access' => $networkFolderAccess,
            'form' => $form->createView(),
        ]);
    }

你看过这些 tutorials on symfonyCast 了吗?完全符合你的需求(从第18章到最后)

它将向您展示如何使用 OneToMany 关系、表单原型和集合类型来添加 and/or 删除嵌入表单。