0
votes

Here is what I have tried:

  $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()) {

Handle request does not submit form. If I call submit after handle request, it still does not validate.

I even tried

 /** @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])
            ]);

But validate gives error:

Expected argument of type "array or \Countable", "Symfony\Component\HttpFoundation\File\UploadedFile" given.

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...', Object(Symfony\Component\Validator\Mapping\GenericMetadata), 'Default', Object(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...', Object(Symfony\Component\Validator\Mapping\GenericMetadata), '', Array, NULL, 1, Object(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, Object(Symfony\Component\HttpFoundation\Request), Object(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->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)

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

10 {main}

I see this is not as in documentation example, but I want to not refactor much if possible. This should be simple fix but I spent few hours and cannot find.

1
Can you show the exception stack trace (i.e. where exactly is the exception being thrown)? - xabbuh
@xabbuh - updated question - Darius.V
Now I see that the error comes from new Count(). When I remove count, then it is fine. But how do I validate that it is one file? - Darius.V

1 Answers

0
votes
$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])
            ]);

The problem was new Count. It is not needed because for validate method if we upload multiple files, then $f will be array and there will be exception, so user will not be able to upload multiple files.