I’m working on a Symfony 6 application on PHP 8.1.
I’m trying to validate a custom DTO which has the properties marked with Assert attributes.
Here’s my DTO class
use JMSSerializerAnnotation as JMS;
use SymfonyComponentValidatorConstraints as Assert;
class JWTData
{
#[JMSType('string')]
#[JMSSerializedName('UserName')]
#[AssertNotBlank]
#[AssertNotNull]
private string $userName;
}
And that’s the code I’m using to validate the DTO
$dto = $this->serializer->deserialize(json_encode($userDataArray), JWTData::class, 'json');
$errors = $this->validator->validate($dto);
Even if I provide an empty array as data, the validator not fires any constraint. Digging in the validator component, I discovered that the metadata related to my DTO class not contains the constraints added with the Assert attribute.
This is the Symfony’s configuration about validation
framework:
validation:
enabled: true
enable_annotations: true
email_validation_mode: html5
I tried also to debug the validator with this command php bin/console debug:validator "AppDtoAuthenticationJWTData" and the result shows correctly the constraints on the property.
What I’m missing? How can I validate correctly my class?
