这是我尝试过的:
$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()) {
句柄请求未提交表单。如果我在句柄请求后调用Submit,它仍然无法验证。
我什至尝试
/** @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])
]);
但是验证给出了错误:
给出了“数组或\ 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(Object(Symfony \ Component \ HttpFoundation \ Request), 1)
8 /home/darius/PhpstormProjects/surplus/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(202):
Symfony \ Component \ HttpKernel \ HttpKernel->句柄(对象(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 {main}
我看到这与文档示例中的内容不同,但是如果可能的话,我不希望重构太多。这应该是简单的解决方法,但我花了几个小时却找不到。
答案 0 :(得分:0)
$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将是数组,并且会有异常,因此用户将无法上传多个文件。