错误约束上传图像文件Symfony 5
我尝试使用Symfony 5.4及其文档上传文件。提交表单时,我的表单projectType 的约束消息请上传有效的JPG/PNG文件
,而它是一个PNG文件。
这是我的代码:
// Entity\Project.php
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
// =============================================
// Form\ProjectType.php
->add('image', FileType::class , [
"attr" => [
"class" => "form-control"
],
"required" => "false",
"mapped" => true ,
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'application/png',
'application/jpeg',
],
'mimeTypesMessage' => 'Please upload a valid JPG/PNG file',
])
],
])
// =====================================================
// Controller\ProjectController.php
if($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager() ;
// Image file
$img = $form->get('image')->getData() ;
if($img){
$original = pathinfo($img->getClientOriginalName() , PATHINFO_FILENAME) ;
$saveFile = $slugger->slug($original) ;
$newFileName = $safeFileName.'-'.uniqid().'.'.$img->guessExtension() ;
try{
$img->move(
$this->getParameter('image_file_directory'),
$newFileName
) ;
catch (FileException $e){
// ... handle exception if something happens during file upload
}
}
$project->setImage($newFileName) ;
I try to upload files with Symfony 5.4 and its documentation. When I submit my form, I have the constraint message of my form ProjectType Please upload a valid JPG/PNG file
whereas it's a png file..
Here my code:
// Entity\Project.php
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
// =============================================
// Form\ProjectType.php
->add('image', FileType::class , [
"attr" => [
"class" => "form-control"
],
"required" => "false",
"mapped" => true ,
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'application/png',
'application/jpeg',
],
'mimeTypesMessage' => 'Please upload a valid JPG/PNG file',
])
],
])
// =====================================================
// Controller\ProjectController.php
if($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager() ;
// Image file
$img = $form->get('image')->getData() ;
if($img){
$original = pathinfo($img->getClientOriginalName() , PATHINFO_FILENAME) ;
$saveFile = $slugger->slug($original) ;
$newFileName = $safeFileName.'-'.uniqid().'.'.$img->guessExtension() ;
try{
$img->move(
$this->getParameter('image_file_directory'),
$newFileName
) ;
catch (FileException $e){
// ... handle exception if something happens during file upload
}
}
$project->setImage($newFileName) ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
而不是
实际上使用,有些扩展具有许多相应的模拟型。理想情况下,您需要列出所有内容,否则用户可以上传 *.jpg文件,并且仍然不会接受。
,您可以创建一个函数(您可以将$ FILETYPES数组提取到配置):
$ label的翻译值。
为了使其保持简单且可扩展以后 使用它像pimcore一样,适用于符号和框架。
You need to use
instead of
Indeed, some extensions have many corresponding mimetypes. Ideally, you need to list them all otherwise user can upload *.jpg file and it still would not be accepted.
To keep it simple and extendable for future, you can create a function like that (you can extract $fileTypes array to configuration):
translation value for
$label.'help'
should be likeMy sample should work fine for Symfony and frameworks using it like Pimcore.