Symfony 3.4 文件在使用 createFormBuilder 时未经过验证

Posted

技术标签:

【中文标题】Symfony 3.4 文件在使用 createFormBuilder 时未经过验证【英文标题】:Symfony 3.4 file is not validated when using createFormBuilder 【发布时间】:2019-01-21 07:24:26 【问题描述】:

这是我尝试过的:

  $form = $this->createFormBuilder(null, ['method' => 'POST', 'csrf_protection' => false])
            ->add('file', FileType::class, [
                'required' => true,
                'constraints' => [
                    new File([
                        'mimeTypes' => [
                            'application/pdf',
                            'application/msword',
                            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                            'application/vnd.ms-excel',
                            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                            'image/gif',
                            'image/png',
                            'image/jpeg',
                            'application/vnd.oasis.opendocument.text',
                            'application/vnd.oasis.opendocument.spreadsheet'
                        ]
                    ]),
                    new Count(['min' => 1, 'max' => 1])
                ]
            ])
            //->add('submit', SubmitType::class)
            ->getForm();





        $form->handleRequest($request);
//        $form->submit($request->request->all(), false);
        $form->submit($request->files->get($form->getName()));

//        $this->


//        if ($form->isValid()) 
        if (/*$form->isSubmitted() &&*/ $form->isValid()) 

处理请求不提交表单。如果我在处理请求后调用提交,它仍然无法验证。

我什至尝试过

 /** @var UploadedFile $f */
            $f = $request->files->get('file');



            $violations = $this->container->get('validator')->validate($f, [
                new File([
                    'mimeTypes' => [
                        'application/pdf',
                        'application/msword',
                        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                        'application/vnd.ms-excel',
                        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        'image/gif',
                        'image/png',
                        'image/jpeg',
                        'application/vnd.oasis.opendocument.text',
                        'application/vnd.oasis.opendocument.spreadsheet'
                    ]
                ]),
                new Count(['min' => 1, 'max' => 1])
            ]);

但验证会出错:

给定类型为“array or \Countable”、“Symfony\Component\HttpFoundation\File\UploadedFile”的预期参数。

0 /home/darius/phpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php(829):

Symfony\Component\Validator\Constraints\CountValidator->validate(Object(Symfony\Component\HttpFoundation\File\UploadedFile), Object(Symfony\Component\Validator\Constraints\Count))

1 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php(675):

Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateInGroup(Object(Symfony\Component\HttpFoundation\File\UploadedFile), '000000006bcba97...', 对象(Symfony\Component\Validator\Mapping\GenericMetadata), '默认', 对象(Symfony\Component\Validator\Context\ExecutionContext))

2 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php(118):

Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateGenericNode(Object(Symfony\Component\HttpFoundation\File\UploadedFile), NULL, '000000006bcba97...', 对象(Symfony\Component\Validator\Mapping\GenericMetadata),'', 数组,NULL,1, 对象(Symfony\Component\Validator\Context\ExecutionContext))

3 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/RecursiveValidator.php(100):

Symfony\Component\Validator\Validator\RecursiveContextualValidator->validate(Object(Symfony\Component\HttpFoundation\File\UploadedFile), Array, Array)

4 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/Validator/Validator/TraceableValidator.php(65):

Symfony\Component\Validator\Validator\RecursiveValidator->validate(Object(Symfony\Component\HttpFoundation\File\UploadedFile), Array, NULL)

5 /home/darius/PhpstormProjects/surplus/src/STL/TaxCalculatorBundle/Controller/Api/TaxCalculatorController.php(357):

Symfony\Component\Validator\Validator\TraceableValidator->validate(Object(Symfony\Component\HttpFoundation\File\UploadedFile), Array)

6 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(151):

STL\TaxCalculatorBundle\Controller\Api\TaxCalculatorController->uploadAction(300, 对象(Symfony\Component\HttpFoundation\Request), 对象(FOS\RestBundle\Request\ParamFetcher))

7 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(68):

Symfony\Component\HttpKernel\HttpKernel->handleRaw(对象(Symfony\Component\HttpFoundation\Request), 1)

8 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(202):

Symfony\Component\HttpKernel\HttpKernel->handle(对象(Symfony\Component\HttpFoundation\Request), 1、真)

9 /home/darius/PhpstormProjects/surplus/web/app_dev.php(32): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))

10 主

我发现这与文档示例中的情况不同,但如果可能的话,我不想进行太多重构。这应该很简单,但我花了几个小时却找不到。

【问题讨论】:

你能显示异常堆栈跟踪吗(即异常究竟是在哪里抛出的)? @xabbuh - 更新问题 现在我看到错误来自 new Count()。当我删除计数时,就可以了。但是如何验证它是一个文件? 【参考方案1】:
$violations = $this->container->get('validator')->validate($f, [
                new File([
                    'mimeTypes' => [
                        'application/pdf',
                        'application/msword',
                        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                        'application/vnd.ms-excel',
                        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        'image/gif',
                        'image/png',
                        'image/jpeg',
                        'application/vnd.oasis.opendocument.text',
                        'application/vnd.oasis.opendocument.spreadsheet'
                    ]
                ]),
                //new Count(['min' => 1, 'max' => 1])
            ]);

问题是新的计数。不需要,因为 validate 方法如果我们上传多个文件,那么 $f 将是数组并且会有异常,所以用户将无法上传多个文件。

【讨论】:

以上是关于Symfony 3.4 文件在使用 createFormBuilder 时未经过验证的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 3.4 - 将 API 流文件传输到客户端下载

Hwi oauth bundle 和 Symfony 3.4 无法自动装配服务:如何在 symfony 3.4 + FOSUserBundle 中使用 hwi/oauth-bundle

Symfony 3.4 在我的包中使用视图

在命令 symfony 3.4 上获取项目目录

Symfony 3.4 安装期间的错误 php json

Symfony 3.4 功能测试