I am attempting to upload a file via an API endpoint controller I have created:
/**
* @Route("/upload", methods="POST")
*/
public function upload(Request $request)
{
$form = $this->createForm(UserFileType::class);
$form->handleRequest($request);
if (!$form->isSubmitted()) {
dd($request->files->get('file'));
}
...
The dd($request->files->get('file')) is showing my file as expected so I am unclear why isSubmitted() is returning false when the method is receiving multipart/form-data POST request with data. I am submitting the POST request via Postman. Why is the form not submitted?
UserFileType:
class UserFileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class, [
'mapped' => false,
'required' => true,
'constraints' => [
new File([
'maxSize' => '2M',
'mimeTypes' => [
'application/pdf',
'application/x-pdf',
],
'maxSizeMessage' => 'The file size must not exceed {{ limit }} {{ suffix }}.',
'mimeTypesMessage' => 'The file type {{ type }} is not valid',
])
],
]);
}
UserFileType? There could be a mismatch in the fields defined there and that in your request. Either that or a mismatch in the method on theUserFileTypeform class.UserFileType?$builder->setMethod('POST')->add('...')$form->getErrors()output and add it if necessary please.