3

我有一个包含 FileType 字段的表单。我已将multiple选项设置为,true以便用户可以同时上传多个文件。

$builder->add('myFile', FileType::class, [
                'label' => 'upload file',
                'multiple' => true,
])

这是连接到此表单的实体中的相应属性:

     /**
     * @Assert\NotBlank()
     * @Assert\File(mimeTypes = {"application/pdf", "application/x-pdf", "image/jpeg", "image/png"})
     * @ORM\Column(type="array")
     */
     private $myFile;

当我提交表单时,我收到错误:

UnexpectedTypeException in FileValidator.php line 168:
Expected argument of type "string", "array" given

我在 File assert 前面添加了花括号,所以它看起来像这样:

* @Assert\File{}(mimeTypes = {"application/pdf", "application/x-pdf", "image/jpeg", "image/png"})

现在提交表单时它不会抱怨。但也不检查文件类型验证。

知道如何使文件类型适用于多个选定的文件吗?

4

1 回答 1

2

由于您正在验证 的数组File,因此您需要应用All验证器,它将在数组的每个元素上应用内部验证器。

尝试类似:

/**
 * @Assert\All({
 *     @Assert\NotBlank()
 *     @Assert\File(mimeTypes = {"application/pdf", "application/x-pdf", "image/jpeg", "image/png"})
 * })
 * @ORM\Column(type="array")
 */
 private $myFile;
于 2017-09-07T10:42:28.020 回答