3

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',
                    ])
                ],
            ]);
    }
6
  • What is 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 the UserFileType form class. Commented Dec 22, 2020 at 20:59
  • @OluwafemiSule please see edit adding UserFileType. Commented Dec 22, 2020 at 21:05
  • What do you get setting the form method when building the form in UserFileType? $builder->setMethod('POST')->add('...') Commented Dec 22, 2020 at 21:29
  • After adding ->setMethod('POST') to $builder and posting the file isSubmitted still returns false Commented Dec 22, 2020 at 21:40
  • Could you check $form->getErrors() output and add it if necessary please. Commented Dec 22, 2020 at 22:25

2 Answers 2

4

For form classes derived from AbstractType, the form is named using fqcnToBlockPrefix when it's built through FormFactory.

You can confirm this by dumping $form->getName().

Now, $form->handleRequest method as implemented performs a number of checks through the request handler before submitting the form.

  1. Checks the method matches that of the form
  2. Checks the name of the form is present in the request if the form has a name
  3. Checks that at least one of the form fields are filled if the form has no name

Your form is not submitting through the request handler handleRequest method because fields in the request are not properly mapped together with your form name.

You have to map the form name in your HTTP POST request in following way:

[ "user_file" =>  [ "file" => <uploaded file> ] ]

This may prove to not be straight forward in Postman. Therefore I recommend to implement your form as a nameless form instead by overriding getBlockPrefix to return an empty string.


class UserFileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //...
    }

    public function getBlockPrefix()
    {
        return '';
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Since this is the first SO thread that comes up for the keywords handlerequest issubmitted false, I thought I would share what worked for my team on an adjacent issue.

We were getting a false response when doing a PATCH request. We eventually realized that we were neglecting to pass Request::METHOD_PATCH in the method option when creatin the form. Putting in a ternary statement that set that value was enough to solve our problem.

I hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.