Foreach FileType in Form : symfony 4.4

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

我的数据库中有一个文件列表(身份证、护照......),我想在我的 "InterimaireType "中创建一个和我的文件一样多的FileType。

Controller/InterimaireController

    public function add(Request $request, InterimaireManager $interimaireManager, ListeDocumentRepository $listeDocumentRepository) : Response
    {
        $listeDocuments = $listeDocumentRepository->findAll();

        $form = $this->createForm(InterimaireFormType::class, null, [
            'data' => $listeDocuments
        ]);
        $form->handleRequest($request);

        return $this->render('interimaire/add.html.twig', [
            'interimaire_form' => $form->createView(),
        ]);
    }

dump "$listeDocuments"

InterimaireController.php on line 81:
array:2 [▼
  0 => App\Entity\ListeDocument {#1181 ▼
    -id: 16
    -libelle: "ID card"
    -created_at: DateTime @1576461552 {#1178 ▶}
    -modified_at: DateTime @1588272832 {#1179 ▶}
    -interimaireFiles: Doctrine\ORM\PersistentCollection {#1245 ▶}
  }
  1 => App\Entity\ListeDocument {#1250 ▼
    -id: 17
    -libelle: "Driving licence"
    -created_at: DateTime @1581921759 {#1248 ▶}
    -modified_at: DateTime @1588272832 {#1249 ▶}
    -interimaireFiles: Doctrine\ORM\PersistentCollection {#1251 ▶}
  }
]

Form/InterimaireFormType

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname', TextType::class, [
            ])
            ->add('lastname', TextType::class, [
            ])
            ->add('phone', TelType::class, [
                'required' => false,
                'attr' => [
                    'class' => 'phone'
                ]
            ])
            ->add('email', EmailType::class, [
                'required' => false,
            ])
        ;

        $listeDocuments = $builder->getData();
        if (isset($listeDocuments) && !empty($listeDocuments)) {
            foreach ($listeDocuments as $key => $listeDocument) {
                $builder
                    ->add(Urlizer::urlize($listeDocument->getLibelle()), FileType::class, [
                        'label' => $listeDocument->getLibelle(),
                        'mapped' => false,
                        'required' => false,
                        'multiple' => false,
                        'attr' => [
                            'lang' => 'fr',
                        ]
                    ])
                ;
            }
        }

        $builder
            ->add('save', SubmitType::class, [
                'label' => 'form.interimaire.save.label',
            ])
        ;
    }

谢谢您的帮助:)

ps : 对不起我的英语 :p

php symfony4
1个回答
0
投票

你应该通过 CollectionType 而不是 foreach 循环。

我想这样就可以解决你的问题了(或者你只改一点点)。


        $builder
            ->add('firstname', TextType::class, [
            ])
            ->add('lastname', TextType::class, [
            ])
            ->add('phone', TelType::class, [
                'required' => false,
                'attr' => [
                    'class' => 'phone'
                ]
            ])
            ->add('email', EmailType::class, [
                'required' => false,
            ])
            ->add('files', CollectionType::class, [ // You should care the "files" key. It may be difference at your Entity. I just guess it.
                'entry_type' => FileType::class,
                'label' => 'false',
                'mapped' => false,
                'required' => false,
                'multiple' => false,
                'attr' => [
                    'lang' => 'fr',
                ]
            ])
            ->add('save', SubmitType::class, [
                'label' => 'form.interimaire.save.label',
            ])
        ;
        ;
© www.soinside.com 2019 - 2024. All rights reserved.