Тип файла Foreach в форме: symfony 4.4 - PullRequest
0 голосов
/ 01 мая 2020

У меня есть список файлов (удостоверение личности, паспорт, ....) в моей базе данных, и я хотел бы, чтобы в моем «InterimaireType» было создано столько FileType, сколько у меня есть документов.

Контроллер \ 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: простите за мой английский sh: p

1 Ответ

0 голосов
/ 03 мая 2020

Вы должны сделать это через CollectionType вместо foreach l oop.

Я думаю, что это решит вашу проблему (или вы немного изменились):


        $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',
            ])
        ;
        ;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...