Prevent default image deletion with Easyadmin 3
May 23, 2021
•
•
1
min to read


Depending on your use case, you can face a problem where an image is deleted when a row containing an image is deleted.
First of all, you can override the default type of the image. Using the setFormType
method, you can set the ImageField
to a FileUploadType
like the example bellow :
public function configureFields(string $pageName): iterable
{
return [
ImageField::new('image')
->setFormType(FileUploadType::class)
}
Then, if you take a closer look at the FileUploadType
on the configureOptions
method, you will see some parameters that can be overrided.
$resolver->setDefaults([
'upload_dir' => $this->projectDir.'/public/uploads/files/',
'upload_new' => $uploadNew,
'upload_delete' => $uploadDelete,
'upload_filename' => $uploadFilename,
'upload_validate' => $uploadValidate,
'download_path' => $downloadPath,
'allow_add' => $allowAdd,
'allow_delete' => true,
'data_class' => $dataClass,
'empty_data' => $emptyData,
'multiple' => false,
'required' => false,
'error_bubbling' => false,
'allow_file_upload' => true,
]);
The last you need to do is to override the default function for the upload_delete
option. Now you just need to override this callable using the function setFormTypeOption
like this :
public function configureFields(string $pageName): iterable
{
return [
ImageField::new('image')
->setFormType(FileUploadType::class)
->setFormTypeOption('allow_delete', false)
->setFormTypeOption('upload_delete', function(File $file) {})
}
You can also set the allow_delete
to false
using the same method.
Cover photo by Andrew Neel