I'm working on my module and one part is where you can add new backgrounds to the database to use in a slideshow.
I have a SlideshowBackground entity with 3 properties:
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=150)
*/
private $image;
/**
* @ORM\Column(type="string", length=100)
*/
private $imageAlt;
And basically, the image is the string of the uploaded file, including the relative path.
I have the following SlideshowBackgroundType as form for the adding slideshow background page:
class SlideshowBackgroundType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('image', FileType::class, [
'required' => true
]);
$builder->add('imageAlt', TextType::class, ['required' => true]);
}
}
However, if I submit my form and check $form->getData(), it contains the following data:
SlideshowBackground {#419 ▼
-id: null
-image: "C:\wamp64\tmp\php5B96.tmp"
-imageAlt: "ty"
}
However, this way I can't get the original uploaded file name. I use the following code in my controller:
$slideshowBackground = new SlideshowBackground();
$form = $this->createForm(SlideshowBackgroundType::class, $slideshowBackground);
$form->handleRequest($request);
How can I use SlideshowBackground entity but also be able to have a file uploaded that's not directly mapped to the image?
I tried to modify my image field to the following:
$builder->add('image', FileType::class, [
'required' => false,
'mapped' => false
]);
But then I get the same result from $form->getData(), however with the image column as null.
Also, when I try to submit with the last code, I get the following error on image:
This value should not be null.
I know the image property in SlideshowBackground is required, and it should be, but it doesn't work as image isn't defined when submitting with the mapped => false. Any way to go around this problem?
imagewhich is required, and since when submitting it is null, it throws errors. However,imageshould be required AFAIK as backgrounds need to have an image specified.imageinput in the form builder toimageupload, it does add a new unmapped input type. However, my form doesn't work asimageinSlideshowBackgroundentity is null which cannot happen. Thus,$form->isSubmitted() && $form->isValid()will return false. I only detected me looking at$form->getDataand that only returns the entity (and not extra fields). So I'm able to get the uploaded image from the form but I cannot submit my form