I use DTOs as the data_class
for Symfony form types. There is one thing that does not work for me when I use typed properties (PHP 7.4) in these DTOs.
EXAMPLE:
class ProductDto
{
/*
* @Assert\NotBlank
*/
public string $title;
}
This generally seems to work quite well – in case the user submits the form with a blank title or description, the validation kicks in and the form is displayed with validation warnings.
BUT THERE IS A PROBLEM when data is added while creating a form (e.g. the edit form):
$productDto = new ProductDto();
$productDto->title = 'Foo';
$form = $this->createForm(ProductFormType::class, $productDto);
Initially the form is displayed as expected with Foo
as the value for the title
. When a user clears the title
input form field and submits the form an exception like this is thrown:
Typed property `App\Form\Dto\ProductDto::$title` must be string, null used
As far as I can see this is caused by the fact that during Form->handleRequest()
the title is set to null
after it was set to "Foo" before, right?
Is there a solution for this problem?