【问题标题】:File constraints on Symfony 4 form doesn't workSymfony 4 表单上的文件限制不起作用
【发布时间】:2020-03-16 05:59:46
【问题描述】:

我正在尝试对表单的文件上传字段添加约束。

这是我的表单类型的一部分:

use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Validator\Constraints\File;

class MovieType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('images', FileType::class, [
                'label' => 'Image(s)',
                'multiple' => true,
                'required' => false,
                'constraints' => [
                    new File([
                        'maxSize' => '4M',
                        'mimeTypes' => [
                            'image/jpeg',
                        ],
                        'mimeTypesMessage' => 'Please upload a valid image. '
                    ])
                ]
            ])
        ;
    }
}

它确实添加了验证,但是当我尝试上传 2.5Mb jpeg 时,消息如下:文件太大。它的大小不应超过 2 MiB。并且这个值必须是字符串类型。

我的课也很简单……

/**
 * @MongoDB\Field(type="collection")
 */
private $images;

    public function getImages()
{
    return $this->images;
}

public function setImages($images)
{
    $this->images = $images;

    return $this;
}

我只在存储到MongoDB文档后才保留文件名。

【问题讨论】:

    标签: mongodb symfony file-upload


    【解决方案1】:

    也许您的 php.ini 中的 upload_max_filesize 值设置为 2MB。 如果不是这样,请尝试:

    'maxSize' => '4000000'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2020-02-13
      • 2017-02-27
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多