Bonjour à tous,
Je suis dans une vraie galère avec l'upload de fichiers via le form
J'essaie d'uploader plusieurs fichiers Excel dans un formulaire afin de le lire et d'en générer un autre par la suite.
J'ai donc créé des forms tels que suit :
La vue (twig):
{% extends 'PDCFrontBundle:Default:general.html.twig' %}
{% block body %}
<div class="upload_container">
<p>Veuillez insérer les différents fichiers ci-dessous</p><br>
<div class="upload_tile col-md-2" id="plan_de_charge_client">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Plan de Charge Client</h3>
</div>
<div class="panel-body">
{{ form_start(formPDC) }}
{{ form_widget(formPDC) }}
{{ form_end(formPDC) }}
</div>
</div>
</div>
<div class="upload_tile col-md-2" id="realise_BO">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Réalisé BO</h3>
</div>
<div class="panel-body">
{{ form_start(formBO) }}
{{ form_widget(formBO) }}
{{ form_end(formBO) }}
</div>
</div>
</div>
<div class="upload_tile col-md-2" id="factu">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Facturation</h3>
</div>
<div class="panel-body">
{{ form_start(formFactu) }}
{{ form_widget(formFactu) }}
{{ form_end(formFactu) }}
</div>
</div>
</div>
<div class="upload_tile col-md-2" id="rtc">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Fichier RTC</h3>
</div>
<div class="panel-body">
{{ form_start(formRTC) }}
{{ form_widget(formRTC) }}
{{ form_end(formRTC) }}
</div>
</div>
</div>
</div>
{% endblock %}
Le controller:
<?php
namespace PDC\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class LoadFileController extends Controller
{
public function indexAction()
{
/**
* Création de tous les formulaires via la fonction createUploadForm
*/
$formPDC = $this->createUploadASSUForm();
$formBO = $this->createUploadBOForm();
$formFactu = $this->createUploadATOSForm();
$formRTC = $this->createUploadRTCForm();
/**
* Traitement du fichier uploadé
*/
if ($formPDC->isSubmitted() && $formPDC->isValid()) {
$fileUploaded = new UploadedFile($formPDC['file']->getData('pathName'),
$formPDC['file']->getData('originalName'),
$formPDC['file']->getData('mimeType'));
$fileName = $this->fileUploadProcess($fileUploaded);
$fileSource = $formPDC->get('perimetre')->getData();
$this->forward('PDCFrontBundle:Demande:add', [
'fileSource'=> $fileSource,
'filePerimetre' => 'ASSU',
'fileName' => $fileName
]);
} elseif ($formBO->isSubmitted() && $formBO->isValid()) {
$fileUploaded = new UploadedFile($formBO['file']->getData('pathName'),
$formPDC['file']->getData('originalName'),
$formPDC['file']->getData('mimeType'));
$fileName = $this->fileUploadProcess($fileUploaded);
$fileSource = $formBO->get('perimetre')->getData();
$this->forward('PDCFrontBundle:Demande:add', [
'fileSource' => $fileSource,
'filePerimetre' => 'BO',
'fileName' => $fileName
]);
} elseif ($formFactu->isSubmitted() && $formFactu->isValid()) {
$fileUploaded = new UploadedFile($formFactu['file']->getData('pathName'),
$formPDC['file']->getData('originalName'),
$formPDC['file']->getData('mimeType'));
$fileName = $this->fileUploadProcess($fileUploaded);
$fileSource = $formFactu->get('perimetre')->getData();
$this->forward('PDCFrontBundle:Demande:add', [
'fileSource'=> $fileSource,
'filePerimetre' => 'ATOS',
'fileName' => $fileName
]);
} elseif ($formRTC->isSubmitted() && $formRTC->isValid()) {
$fileUploaded = new UploadedFile($formRTC['file']->getData('pathName'),
$formPDC['file']->getData('originalName'),
$formPDC['file']->getData('mimeType'));
$fileName = $this->fileUploadProcess($fileUploaded);
$fileSource = $formRTC->get('perimetre')->getData();
$this->forward('PDCFrontBundle:Demande:add', [
'fileSource' => $fileSource,
'filePerimetre' => 'RTC',
'fileName' => $fileName
]);
}
/**
* Envoi de tous les formulaires au twig
*/
return $this->render('PDCFrontBundle:File_upload:upload.html.twig', [
'formPDC' => $formPDC->createView(),
'formBO' => $formBO->createView(),
'formFactu' => $formFactu->createView(),
'formRTC' => $formRTC->createView()
]);
}
private function createUploadASSUForm()
{
$builder = $this->createFormBuilder();
$pdcFormOption = [
'label' => false,
'attr' => [
'style' => 'color:transparent',
'onchange' => 'this.style.color = \'black\';'
]
];
$perimetreOptions = [
'choices' => [
'SGKP' => 'AS400 SGKP',
'SGR' => 'AS400 SGR',
'JEE' => 'JEE'
],
'label' => false
];
$submitOptions = [
'label' => 'Traiter'
];
$builder->add('file', FileType::class, $pdcFormOption)
->add('perimetre', ChoiceType::class, $perimetreOptions)
->add('validate', SubmitType::class, $submitOptions);
$form = $builder->getForm();
$form->handleRequest($this->get('request'));
return $form;
}
private function createUploadBOForm()
{
$builder = $this->createFormBuilder();
$boFormOption = [
'label' => false,
'attr' => [
'style' => 'color:transparent',
'onchange' => 'this.style.color = \'black\';'
]
];
$perimetreOptions = [
'choices' => [
'SGKP' => 'AS400 SGKP',
'SGR' => 'AS400 SGR',
'JEE' => 'JEE'
],
'label' => false
];
$submitOptions = [
'label' => 'Traiter'
];
$builder->add('file', FileType::class, $boFormOption)
->add('perimetre', ChoiceType::class, $perimetreOptions)
->add('validate', SubmitType::class, $submitOptions);
$form = $builder->getForm();
$form->handleRequest($this->get('request'));
return $form;
}
private function createUploadATOSForm()
{
$builder = $this->createFormBuilder();
$factuFormOption = [
'label' => false,
'attr' => [
'style' => 'color:transparent',
'onchange' => 'this.style.color = \'black\';'
]
];
$perimetreOptions = [
'choices' => [
'SGKP' => 'AS400 SGKP',
'SGR' => 'AS400 SGR',
'JEE' => 'JEE'
],
'label' => false
];
$submitOptions = [
'label' => 'Traiter'
];
$builder->add('file', FileType::class, $factuFormOption)
->add('perimetre', ChoiceType::class, $perimetreOptions)
->add('validate', SubmitType::class, $submitOptions);
$form = $builder->getForm();
$form->handleRequest($this->get('request'));
return $form;
}
private function createUploadRTCForm()
{
$builder = $this->createFormBuilder();
$rtcFormOption = [
'label' => false,
'attr' => [
'style' => 'color:transparent',
'onchange' => 'this.style.color = \'black\';'
]
];
$perimetreOptions = [
'choices' => [
'SGKP' => 'AS400 SGKP',
'SGR' => 'AS400 SGR',
'JEE' => 'JEE'
],
'label' => false
];
$submitOptions = [
'label' => 'Traiter'
];
$builder->add('file', FileType::class, $rtcFormOption)
->add('perimetre', ChoiceType::class, $perimetreOptions)
->add('validate', SubmitType::class, $submitOptions);
$form = $builder->getForm();
$form->handleRequest($this->get('request'));
return $form;
}
/**
* Function used to manage file upload and DemandeController redirection
*
* @param UploadedFile $inputFileObj2Process
* @return mixed
*/
private function fileUploadProcess(UploadedFile $inputFileObj2Process)
{
$dir = 'C:\wamp64\www\PDC_sf2\uploads';
$fileExtension = $inputFileObj2Process->guessExtension();
$fileName = str_replace(".tmp", ".".$fileExtension, $inputFileObj2Process->getClientOriginalName());
if ($inputFileObj2Process->isValid()) {
$inputFileObj2Process->move($dir, $fileName);
}
return $fileName;
}
}
Le problème c'est que je ne sais pas du tout comment gérer ces formulaires (du moins lorsque l'on soumet un fichier) car lorsque je soumet l'un des formulaires c'est toujours le premier et uniquement le premier qu'il prends en compte ce qui me génère des erreurs par la suite dans mon code.
Je suis allé voir les docs sur le site de Symfony mais j'avoue ne pas avoir très bien saisi le fonctionnement de la soumission et du traitement d'un fichier dans un formulaire.
NB: Je lis bien les fichiers que j'uploade. J'utilise un bundle PHPExcel / PHPOffice pour lire le contenu du fichier par la suite. Ce que je cherches c'est un moyen de gérer proprement l'uplaod de mes fichiers.
En fait, tu as plusieurs form là.
Si tu souhait un seul formulaire tu dois avoir un structure du genre :
{{ form_start() }}
TOUT LES CHAMPS...
{{ form_end()