1

Hello to the community,

I'm building a portofolio page where upload images, i have created a class Portofolio where i save a Collection of a class Image.

class PortofolioPage


/**
* @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="natureGallery", cascade={"persist"})
 */
private $natureGallery;

and my form type:

use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Form\Extension\Core\Type\FileType;

->add('natureGallery', FileType::class, [
            'multiple' => true,
            'label' => 'form.natureGallery',
            'mapped' => false,
            'required' => false,
            'constraints' => [
                new Image([
                    'maxSize' => '5M',
                ])
            ],
        ])

without the constraints section i can upload images but i have a maxSize of 2M, if the contrainst section is active i get an error "This value should be of type string" ( i guess doesn't like whne i create the new Image it gets an array ) and if i deactivate the multiple stills doesn't allow me to upload a more than 2M.

I already modified my php.ini, post_max_size = 50M and upload_max_filesize = 50M and follow the documentation in https://symfony.com/doc/4.4/controller/upload_file.html and checked symfonycast but the solution eludes me.

Thanks for your time and happy coding

  • Maybe this can help you [link](https://stackoverflow.com/questions/56690952/symfony-form-uploaded-file-this-value-should-be-of-type-string) – DaszuOne Apr 30 '20 at 08:44
  • Thanks DaszuOne, I checked the validator.yaml, dataclass => null and use it the assert in the class, nothing works, tried with the multiple option removed. Another thing annoys me is even i specefy the max of 5M when i submit something above it keeps telling me the max is 2M, there is another file where you tell symfony the max of the files? – Ernest Riccetto May 01 '20 at 08:31
  • Nothing else coming to my mind now. I'l try to reproduce your case later, and maybe then we can fix this. You are using symfony 4.4, am I right? – DaszuOne May 01 '20 at 08:47
  • Yes I'm using symfony 4.4 i checked. Here is my project in github https://github.com/erethilclaw/photoBlog I guess is something about my relationship with the classes, not taking well the collections... Checking the symfonyCasts tutorial ( is v5 ) maybe i found something. And Thanks for your help DaszuOne. – Ernest Riccetto May 01 '20 at 09:28
  • Hello @DaszuOne, Using All constraint fixed the string error, but i can't still upload Images above 2M ->add('natureGallery', FileType::class, [ 'multiple' => true, 'label' => 'form.natureGallery', 'mapped' => false, 'required' => false, 'constraints' => new All([ new Image([ 'maxSize' => '5M' ]) ]), ]) and tried in the class @Assert\All(@Assert\Image(maxSize="5M")) – Ernest Riccetto May 02 '20 at 09:00

1 Answers1

1

For multiple file uploader, you should wrap Image constraint into All constraint. That's because submitted value will be collection of objects (link). If you switch multiple option to false, you'll see that your constraint is working... Actually it's not working for you, because you treat submitted data as collection in your controller.

So, for multiple upload add use Symfony\Component\Validator\Constraints\All; and change constraints section to:

new All([
    new Image([
        'maxSize' => '5M'
    ])
])

or for single upload modify your controller (for example):

if (is_array($natureFiles)) {
   ...
} else {
    $filename = $fileUploader->uploadImage($natureFiles);

    $image = new Image();
    $image->setName($filename);
    $image->setUrl($this->getParameter('upload_directory').'natureGallery/'.$filename);

    $portofolio->addNatureGallery($image);
}
DaszuOne
  • 759
  • 1
  • 6
  • 18
  • Hello @DaszuOne, Using All constraint fixed the string error, but i can't still upload Images above 2M ->add('natureGallery', FileType::class, [ 'multiple' => true, 'label' => 'form.natureGallery', 'mapped' => false, 'required' => false, 'constraints' => new All([ new Image([ 'maxSize' => '5M' ]) ]), ]) I get the error 2 times, using the symfony debuger one cames from the validator the other unknown. – Ernest Riccetto May 02 '20 at 09:02
  • And now symfony validator tells you that file is greater than 2M even with setting constraint to 5M? Or error message is different now? Be sure that you are modifying proper php.ini file. Did you check configuration with phpinfo()? I've tested your code and everything goes well. – DaszuOne May 02 '20 at 10:34
  • Same error, The file is too large. Allowed maximum size is 2 MiB. and i get it 2 times, using the debug bar in symfony the cause of the first i don't get any information, the second from the validator. If low the maximun in my constraint below 2M I see my value in the second error ( the one comes fromt the validator). I tried to comment all my validator.yaml and nothing Checked phpInfo(), post and max setted to 50M, I'm looking if https://symfony.com/download has some variable to tweak. – Ernest Riccetto May 02 '20 at 11:44
  • I think the best you can do is to configure xdebug and check what happens inside the validator class. Xdebug is very useful tool so configuring it is not a waste of time ;) – DaszuOne May 02 '20 at 12:35
  • I will try to debug the project with xdebug, i only get this working one time, if I find what's happening i will post it. Thanks DaszuOne. PD. when you say "tested your code" mean you have donwloaded my project ? – Ernest Riccetto May 02 '20 at 14:54
  • Yes, I've runned your project from github. – DaszuOne May 02 '20 at 16:14
  • Ok I fixed, I had to modify the php.ini from the cli folder, which is the one symfony uses to start the server. Still doesn't allows me to upload a certain M in one push but... that will another day and right now for my project upload 20 images is enough. Thanks for your help @DaszuOne I apreciate it a lot. – Ernest Riccetto May 04 '20 at 14:50