Symfony easyadmin3 VICH UPLOADER shows error in form pages (edit & create pages)

please I will really appreciate some help here. I am a very new programmer!

  1. Can someone kindly share any useful link to a very detailed documentation for easyadmin3 (Symfony4) with an image uploader like vich? Somehing different from this (https://symfony.com/doc/2.x/bundles/EasyAdminBundle/integration/vichuploaderbundle.html).

2)I am using easyadmin3 (symfony4) with vichuploader and when I used ImageField::new(‘image’) it throws an errors asking me to specify an upload location using the method setUploadDir(). So I tried the below code and it allows me upload my images but images are not shown on the list page.

public function configureFields(string $pageName): iterable
{
return [

        ImageField::new('image')->onlyOnForms()->setUploadDir('/public/uploads'),
    ];
}
  
  1. Next, i tried the code below and it shows the images on the list page but when I click edit or create new it throws the error: The "imageFile" image field must define the directory where the images are uploaded using the setUploadDir() method. Pleas I need some help here.

public function configureFields(string $pageName): iterable
{
return [

        IntegerField::new('id','id')->hideOnForm(),
        TextField::new('title', 'title')
            ->setHelp('Enter name of category'),
        DateTimeField::new('created_at', 'created_at'),
        TextField::new('image_name', 'image_name')->setHelp('Enter image alternative name'),
      
        
        ImageField::new('image', 'image')->onlyOnIndex()->setBasePath('/uploads')->setLabel('image'),
        ImageField::new('imageFile', 'Image')->onlyOnForms()->setFormType(VichFileType::class)

    ];
}


enter code here
 


Here is my entity:

<?php

namespace AppEntity;

use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
use AppRepositoryCategoryRepository;
use DoctrineORMMapping as ORM;

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

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

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

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

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     * @var string|null
     */
    private $image;

    /**
     * @VichUploadableField(mapping="category_images", fileNameProperty="image")
     * @var File|null
     */
    private $imageFile;


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



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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getCreatedAt(): ?DateTimeInterface
    {
        return $this->created_at;
    }

    public function setCreatedAt(DateTimeInterface $created_at): self
    {
        $this->created_at = $created_at;
        //$this->created_at = new DateTime('now');

        return $this;
    }




    public function getImageName()
    {
        return $this->ImageName;
    }

    public function setImageName($ImageName)
    {
        $this->ImageName = $ImageName;

        return $this;
    }



    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|SymfonyComponentHttpFoundationFileUploadedFile|null $imageFile
     */

    public function setImageFile(?File $imageFile = null): void
    {
        $this->imageFile = $imageFile;

        // VERY IMPORTANT:
        // It is required that at least one field changes if you are using Doctrine,
        // otherwise the event listeners won't be called and the file is lost
        if (null !== $imageFile) {
            // if 'updatedAt' is not defined in your entity, use another property
            $this->updatedAt = new DateTimeImmutable();  //new DateTime('now');
        }
    }

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }

    public function setImage(?string $image): void
    {
        $this->image = $image;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

}



////////////////////////////////////////////////
 config/service.yaml

parameters:
   ##upload_directory: '%kernel.project_dir%/public/uploads'
   app.path.category_images: '/uploads'


///////////////////////////////////////////////////
config/vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mappings:
        category_images:
            uri_prefix: '%app.path.category_images%'
            upload_destination: '%kernel.project_dir%/public/%app.path.category_images%'
            namer: VichUploaderBundleNamingOrignameNamer


///////////////////////////////////////////////////////
Here is my controller

<?php

namespace AppControllerAdmin;

use AppEntityCategory;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldIntegerField;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use EasyCorpBundleEasyAdminBundleFieldDateTimeField;
use EasyCorpBundleEasyAdminBundleFieldImageField;
use EasyCorpBundleEasyAdminBundleFieldFieldTrait;
use EasyCorpBundleEasyAdminBundleFormTypeFileUploadType;
use SymfonyComponentHttpFoundationFileFile; 
use VichUploaderBundleFormTypeVichFileType;
use VichUploaderBundleFormTypeVichImageType;




class CategoryCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Category::class; 
    }
        
      
    public function configureFields(string $pageName): iterable
    {
        return [
           
            IntegerField::new('id','id')->hideOnForm(),
            TextField::new('title', 'title')
                ->setHelp('Enter name of category'),
            DateTimeField::new('created_at', 'created_at'),
            TextField::new('image_name', 'image_name')->setHelp('Enter image alternative name'),           
            ImageField::new('image', 'image')->onlyOnIndex()->setBasePath('/uploads')->setLabel('image'),
            ImageField::new('imageFile', 'Image')->onlyOnForms()->setFormType(VichFileType::class)               
            
        ];
    }
      
}

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

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