symfony-traits icon indicating copy to clipboard operation
symfony-traits copied to clipboard

HandleForm validations not working

Open zmax92 opened this issue 6 months ago • 1 comments

HandleForm() method doesn't validate and throw exceptions where there are asserts on entities or form.

Description

When submitting a form with an image, using HandleForm(), and the required body property is missing, the entity assertions are never checked, so the entity is not populated correctly and may cause an error at runtime, because its expected that entity is correctly populated, because there were no error thrown.

Expected Behavior

When request body is missing required property, there should be exceptions thrown about body missing required property.

Actual Behavior

HandleForm() method doesn't throw any error.

Steps to Reproduce

  1. have a entity with assert on one or more its property
<?php

namespace App\DTO;

use Symfony\Component\Validator\Constraints as Assert;

class Message
{
    #[Assert\NotNull(message: 'message.id.isRequired')]
    private int $userId;
    ...
}
  1. In controller call handleForm() method
#[Route('/path/to/upload', name: 'upload', methods: [Request::METHOD_POST])]
    public function upload(Request $request, Message $message): JsonResponse
    {
        $this->handleForm($request, $message, UploadType::class);

        return $this->json([]), Response::HTTP_CREATED);
    }
  1. In Postman call route, and submit request without form-data body

Code is executed till end, but should throw exception since body property is required and is missing.

zmax92 avatar Jul 26 '24 12:07 zmax92