gpt4 book ai didi

symfony - 多文件验证 : "This value should be of type string"

转载 作者:行者123 更新时间:2023-12-03 20:25:45 25 4
gpt4 key购买 nike

我正在尝试在文件上传表单(表单扩展的验证)上使用 Symfony 验证器,但收到此错误消息:

messageTemplate: "This value should be of type string." from Symfony\Component\Validator\ConstraintViolation



上传在没有验证器的情况下运行良好,我无法弄清楚此消息的来源。

这是我的 FormType,以文档为例进行了基本验证:
    {
$builder
->add('file', FileType::class, [
'label' => 'Choisir un fichier',
'mapped' => false,
'multiple' => true,
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
],
'mimeTypesMessage' => 'Please upload a valid PDF document',
])
],
])
;
}

如果我删除 maxSize , mimeTypes和/或 mimeTypesMessage争论,我仍然有同样的问题。

我不能在实体上使用注释(映射选项设置为 false )。

最佳答案

错误是由于 File需要文件名的约束,但由于该字段具有选项 multiple实际上正在接收一个数组。要解决它,您必须将约束包装在另一个 All 中。约束,这会将内部约束(在本例中为 File)应用于数组的每个元素。

您的代码应如下所示:

    ->add('file', FileType::class, [
'label' => 'Choisir un fichier',
'mapped' => false,
'multiple' => true,
'constraints' => [
new All([
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypesMessage' => 'Please upload a valid PDF document',
'mimeTypes' => [
'application/pdf',
'application/x-pdf'
]
]),
],
]),
]
])

关于symfony - 多文件验证 : "This value should be of type string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61589890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com