我正在尝试在其中具有FileUpload
类型的Symfony4控制器中验证表单。 HTML表单不是用$form->createView()
生成的。
$form->isSubmitted()
始终为假,因为除非执行此操作,否则它不会在请求中看到文件:
$request->request->set("imageFile", $request->files->get('imageFile'));
$form->submit($request->request->all());
简单的HTML表单:
<form style="background-color: #00acc1" method="POST" enctype="multipart/form-data" action="{{ path('user_api_brand_image_upload') }}">
<input type="file" name='imageFile' />
<input type="hidden" name='_token' value="{{ csrf_token('user_api_brand_image_upload') }}" />
<input type=submit value="upload img now">
</form>
控制器:
$formBuilder = $this->createFormBuilder(null, [
'allow_extra_fields' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'user_api_brand_image_upload'
])->add('imageFile', FileType::class, [
'required' => true,
'mapped' => false,
'constraints' => [
new NotBlank(['message' => 'Image is required and must be less than 10MB.']),
new Image([
'maxSize' => '10M'
])
]
]
);
$form = $formBuilder->getForm();
...
//Always false
$form->isSubmitted()
//Unless I do this first
//$request->request->set("imageFile", $request->files->get('imageFile'));
//$form->submit($request->request->all());