I am using Symfony6.4 for an API and recently changed to attributes.
e.g.:
use OpenApiAttributes as OA;
use SymfonyComponentSerializerAnnotationGroups as SerializerGroups;
use SymfonyComponentValidatorConstraints as Assert;
#[OASchema()]
readonly class WorkHour implements DtoInterface
{
public function __construct(
#[OAProperty(description: 'Arbeitsstunden', type: 'integer')]
#[AssertNotNull(message: 'Arbeitsstunden müssen angegeben werden.', groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update', 'collection'])]
public int $hours
) {
}
}
In my unit tests where I try to use null as value for “hours” the expected validation error occurs with http code 422, but the English default text is used: This value should be of type {type}
instead of the German translation, which is available with the validation component. Those files are situated here: vendor/symfony/validator/Resources/translations/validators.de.xlf
Please note: It is also not using my custom message. Using that message would also be fine. I have installed “symfony/translation” via composer.
Since this is an api, I don’t use twig here. The message will be passed to the frontend via the api result.
My translation.yaml looks like this:
framework:
translator:
fallbacks: [ 'de' ]
paths:
- 'vendor/symfony/validator/Resources/translations/'
enabled: true
What do I have to do to make use of the existing default translations for the validator messages or alternatively use my custom messages?