1

I can't upload an image from EasyAdmin V3, I also added VichUpload. I must write setUploadDir() otherwise I have this error message:

'The "illustration" image field must define the directory where the images are uploaded using the setUploadDir() method.

and if I put this method I have this message:

'An error has occurred resolving the options of the form...'

in the meantime I am commenting on lines 161 to 163 vendor\easycorp\easyadmin-bundle\src\Form\Type\FileUploadType.php but I don't want to write in vendors... Do you have an idea for uploading images in EasyAdminV3? I deleted the vich bundle while waiting for a response. Here is my CRUD Controller:

<?php

namespace App\Controller\Admin;

use App\Entity\Planches;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField as ImageFields;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
class PlanchesCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Planches::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('name'),
            SlugField::new('slug')->setTargetFieldName('name'),
            ImageFields::new('illustration')
                    ->setBasePath(' uploads/')
                    ->setUploadDir('public/uploads')
                ->setUploadedFileNamePattern('[randomhash].[extension]')
                ->setRequired(false),
            TextField::new('typedeplanche'),
            TextField::new('marque'),
            IntegerField::new('taille'),
            IntegerField::new('epaisseur'),
            MoneyField::new('prix')->setCurrency('EUR'),
            IntegerField::new('litrage'),
            TextField::new('etatusage'),
            TextField::new('typedederive'),
            TextareaField::new('description'),
            TextField::new('niveau'),
            TextField::new('vendeur'),
            BooleanField::new('vendueavecderives'),
            AssociationField::new('category')
        ];

        }
    
    }

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Aurel_64
  • 11
  • 1
  • 2

2 Answers2

2

There is a typo in your Crud Controller try without vich bundle it works for me ImageFields should be ImageField

public function configureFields(string $pageName): iterable
{
    return [
        TextField::new('name'),
        SlugField::new('slug')->setTargetFieldName('name'),
        ImageField::new('illustration')
            ->setBasePath(' uploads/')
            ->setUploadDir('public/uploads')
            ->setFormType(FileUploadType::class)
            ->setUploadedFileNamePattern('[randomhash].[extension]')
            ->setRequired(false),
        TextField::new('typedeplanche'),
        TextField::new('marque'),
        IntegerField::new('taille'),
        IntegerField::new('epaisseur'),
        MoneyField::new('prix')->setCurrency('EUR'),
        IntegerField::new('litrage'),
        TextField::new('etatusage'),
        TextField::new('typedederive'),
        TextareaField::new('description'),
        TextField::new('niveau'),
        TextField::new('vendeur'),
        BooleanField::new('vendueavecderives'),
        AssociationField::new('category')
    ];

}
0

Watch the spaces between the quote and uploads!

->setBasePath(' uploads/')

Must delete the space otherwise it won't work.

Toni
  • 1,555
  • 4
  • 15
  • 23