[英]Symfony 5, EasyAdmin3 : Field ImageField, How not to delete?
Excuse me for my English.对不起我的英语。
In a form I use the ImageField (esasyadmin 3) like this:在一个表单中,我使用 ImageField (esasyadmin 3),如下所示:
$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.目标是从“public/images”文件夹中选择一个图像。 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
谢谢
sorry for the delay but use this:抱歉耽搁了,但请使用:
$img=ImageField::new('img')
->setBasePath('images')
->setUploadDir('public/images')
->setUploadedFileNamePattern("$name.[extension]")
->setRequired(false);
Again, sorry for the delay but i think it can be usefull to share something about this problem.再次,抱歉耽搁了,但我认为分享一些关于这个问题的东西会很有用。
I had the same problem where i wanted to prevent the default image deletion process when a new image is uploaded.我有同样的问题,我想在上传新图像时阻止默认图像删除过程。 I tried many different methods and i finally figured out what was the problem.
我尝试了许多不同的方法,我终于弄清楚了问题所在。
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:使用
setFormType
方法,您可以将ImageField
设置为FileUploadType
,如下例所示:
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.然后,如果您仔细查看
configureOptions
方法上的FileUploadType
,您会看到一些可以被覆盖的参数。
$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.最后你需要做的是覆盖默认的 function 为
upload_delete
选项。 Now you just need to override this callable using the function setFormTypeOption
like this:现在您只需要使用 function
setFormTypeOption
覆盖这个可调用对象,如下所示:
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.您还可以使用相同的方法将
allow_delete
设置为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.