How to use Vich uploader with easyAdmin 3 on Symfony 5

I’ve been trying to use VichUploader to upload files on a symfony project, already using easyadmin 3.

I’ve configured everything correctly, but I’m getting this error:
"The "pieceJointeFile" image field must define the directory where the images are uploaded using the setUploadDir() method."

<?php

namespace AppEntity;

use AppRepositoryInventaireRepository;
use DateTime;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;

My entity

/**
 * @ORMEntity(repositoryClass=InventaireRepository::class)
 * @VichUploadable
 */
class Inventaire
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=Conducteur::class, inversedBy="inventaires")
     */
    private $conducteur;

    /**
     * @ORMColumn(type="date")
     */
    private $dateInventaire;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $pieceJointe;

    /**
     * @VichUploadableField(mapping="pieceJointe", fileNameProperty="pieceJointe")
     */
    private $pieceJointeFile;

    /**
     * @ORMColumn(type="datetime")
     */
    private $updatedAt;

    public function __construct()
    {
        $this->updatedAt = new DateTime();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getConducteur(): ?Conducteur
    {
        return $this->conducteur;
    }

    public function setConducteur(?Conducteur $conducteur): self
    {
        $this->conducteur = $conducteur;

        return $this;
    }

    public function getDateInventaire(): ?DateTimeInterface
    {
        return $this->dateInventaire;
    }

    public function setDateInventaire(DateTimeInterface $dateInventaire): self
    {
        $this->dateInventaire = $dateInventaire;

        return $this;
    }

    public function getPieceJointeFile() {
        return $this->pieceJointeFile;
    }

    public function setPieceJointeFile($pieceJointeFile): void
    {
        $this->pieceJointeFile = $pieceJointeFile;

        if($pieceJointeFile) {
            $this->updatedAt = new DateTime();
        }
    }

    public function getPieceJointe() {
        return $this->pieceJointe;
    }


    public function setPieceJointe($pieceJointe):self
    {
        $this->pieceJointe = $pieceJointe;
        return $this;
    }

    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

vich uploader configuration

vich_uploader:
    db_driver: orm

    mappings:
        pieceJointe:
            uri_prefix: /files/pieceJointe
            upload_destination: '%kernel.project_dir%/public/files/pieceJointe'

and finally My crud controller

<?php

namespace AppControllerAdmin;

use AppEntityInventaire;
use EasyCorpBundleEasyAdminBundleConfigAction;
use EasyCorpBundleEasyAdminBundleConfigActions;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;
use EasyCorpBundleEasyAdminBundleFieldDateField;
use EasyCorpBundleEasyAdminBundleFieldImageField;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use VichUploaderBundleFormTypeVichFileType;
use VichUploaderBundleFormTypeVichImageType;

class InventaireCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Inventaire::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            DateField::new('dateInventaire'),
            AssociationField::new('conducteur'),
            TextField::new('pieceJointeFile')->setFormType(VichFileType::class, [
                'delete_label' => 'supprimer?'
            ])->onlyOnForms(),
            ImageField::new('pieceJointe')->setBasePath('/files/pieceJointe')->onlyOnDetail(),
            ImageField::new('pieceJointeFile')->setFormType(VichImageType::class)
        ];
    }



    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->add(Crud::PAGE_INDEX, Action::DETAIL);
    }
}

Finally I want to clarify that when using ‘TextField’ it works correctly.
and thank you

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


Lucas Mancini

Lucas Mancini

Hello friend, do you find a solution? I’m stucked in a similiar problem …
in my case:
Expected argument of type “Symfony\Component\HttpFoundation\File\File or null”, “string” given at property path “thumbnailFile”.

TypeError
Argument 1 passed to App\Entity\Shop::setThumbnailFile() must be an instance of Symfony\Component\HttpFoundation\File\File or null, string given, called in /home/ubuntu/environment/vendor/symfony/property-access/PropertyAccessor.php on line 539

I will appreciate any suggestion.