你好,欢迎来到社区。
我正在建立一个上传图片的组合页面,我创建了一个类Portofolio,在这里我保存了一个类Image的集合。
class PortofolioPage
/**
* @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="natureGallery", cascade={"persist"})
*/
private $natureGallery;
和我的表格类型。
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Form\Extension\Core\Type\FileType;
->add('natureGallery', FileType::class, [
'multiple' => true,
'label' => 'form.natureGallery',
'mapped' => false,
'required' => false,
'constraints' => [
new Image([
'maxSize' => '5M',
])
],
])
没有约束部分,我可以上传图片,但我有一个2M的最大尺寸,如果contrainst部分是活动的,我得到一个错误 "这个值应该是字符串类型"(我想不喜欢我创建新的图像,它得到一个数组),如果我停用多个静态照片不允许我上传超过2M的。
我已经修改了我的php.ini,post_max_size = 50M和upload_max_filesize = 50M,并按照文档中的内容进行操作。https:/symfony.comdoc4.4controllerupload_file.html。 并检查了symfonycast,但我无法找到解决方案。
谢谢你的时间,祝你编码愉快
对于多个文件的上传者,你应该将其包裹起来。Image
约束成 All
约束。这是因为提交的值将是对象的集合(联系). 如果你把多个选项切换为false,你会发现你的约束是有效的... ... 事实上,这对你来说并不奏效,因为你将提交的数据视为控制器中的集合。
所以,对于多个上传添加 use Symfony\Component\Validator\Constraints\All;
并将约束条件部分改为。
new All([
new Image([
'maxSize' => '5M'
])
])
或对单个上传修改你的控制器(例如)。
if (is_array($natureFiles)) {
...
} else {
$filename = $fileUploader->uploadImage($natureFiles);
$image = new Image();
$image->setName($filename);
$image->setUrl($this->getParameter('upload_directory').'natureGallery/'.$filename);
$portofolio->addNatureGallery($image);
}