Symfony 5, EasyAdmin3 : Field ImageField, How not to delete?

  easyadmin, imagefield, php, symfony

Excuse me for my English.

In a form I use the ImageField (esasyadmin 3) like this :

$img = ImageField::new('img')
->setBasePath('images)
->setUploadDir('public/images)
->setUploadedFileNamePattern('[name].[extension]')
->setRequired(false)
;

The goal is to choose an image from the “public/images” folder.
However, if I decide to change this image, it removes the image I had previously selected from the folder.
I don’t want this behavior because my image can be used elsewhere.
How to prevent this deletion?
Have I chosen the right type of control?
Thank you

Source: Symfony Questions

One Reply to “Symfony 5, EasyAdmin3 : Field ImageField, How not to delete?”

  • Use `setUploadedFileNamePattern` method.
    For further details check method PHPDoc.
    “`php
    /**
    * @param string|\Closure $patternOrCallable
    *
    * If it’s a string, uploaded files will be renamed according to the given pattern.
    * The pattern can include the following special values:
    * [day] [month] [year] [timestamp]
    * [name] [slug] [extension] [contenthash]
    * [randomhash] [uuid] [ulid]
    * (e.g. [year]/[month]/[day]/[slug]-[contenthash].[extension])
    *
    * If it’s a callable, you will be passed the Symfony’s UploadedFile instance and you must
    * return a string with the new filename.
    * (e.g. fn(UploadedFile $file) => sprintf(‘upload_%d_%s.%s’, random_int(1, 999), $file->getFilename(), $file->guessExtension()))
    */
    “`

LEAVE A COMMENT