I have a problem with my form.
This is the situation:
I use the CraueFormFlowBundle bundle to create a step-by-step form (4 steps).
So, I use the create form of the bundle $form = $flow->createForm();
to create the form.
But when I set up the Symfony file uploader, I can't get the information from the file.
Does anyone have a solution or even a small lead?
My controller:
/**
* @Route("/classified/new", name="classified_create")
* @IsGranted("ROLE_USER")
* @param Request $request
* @param ObjectManager $manager
* @param CreateVehicleFlow $createVehicleFlow
* @param FileUploader $fileUploader
* @return Response
*/
public function create(Request $request, ObjectManager $manager, CreateVehicleFlow $createVehicleFlow, FileUploader $fileUploader)
{
$user = $this->getUser();
$vehicle = new Vehicle();
$vehicle->setCity($user->getCity());
$vehicle->setAuthor($user);
$flow = $createVehicleFlow;
$flow->setGenericFormOptions([
'action' => $this->generateUrl('classified_create')
]);
$flow->bind($vehicle);
$form = $flow->createForm();
if ($flow->isValid($form)) {
$flow->saveCurrentStepData($form);
if ($flow->nextStep()) {
$form = $flow->createForm();
} else {
foreach ($vehicle->getImages() as $image) {
$image->setVehicle($vehicle);
$manager->persist($image);
}
/** @var UploadedFile $imageCoverFile */
$imageCoverFile = $form['imageCover']->getData();
if ($imageCoverFile) {
$imageCoverFileName = $fileUploader->upload($imageCoverFile);
$vehicle->setImageCoverName($imageCoverFileName);
}
$manager->persist($vehicle);
$manager->flush();
$flow->reset();
$this->addFlash(
'success',
"Votre annonce <i>{$vehicle->getId()}</i> a bien été enregistrée"
);
return $this->redirect($this->generateUrl('account_index'));
}
}
return $this->render('front/classified/new_vehicle.html.twig', [
'form' => $form->createView(),
'flow' => $flow,
'vehicle' => $vehicle,
]);
}
My form type:
->add(
'imageCover',
FileType::class, [
'label' => 'Image de couverture',
'mapped' => false,
'required' => false,
'constraints' => [
new File([
'maxSize' => '3M',
'mimeTypes' => [
'application/jpeg',
'application/png',
],
'mimeTypesMessage' => 'Vous devez télécharger un format valide'
])
]
]
);
My entity: I am only the name of the file that is in my entity and only the name of the file that is saved in the database.
/**
* @ORM\Column(type="string", length=255)
*/
private $imageCoverName;
public function getImageCoverName(): ?string
{
return $this->imageCoverName;
}
public function setImageCoverName(string $imageCoverName): self
{
$this->imageCoverName = $imageCoverName;
return $this;
}
I have tried this solution but nothing works.
$imageCoverFile = $form['createVehicle']['imageCover']->getData();