Contributte/Apitte Entity validation and translation error messages

Yess
Member | 16
+
0
-

Hi all, is there some proper way to translate Entity validation msgs? I found out, there is multiple packages / classes within apitte which are playing some role in validation:

My implementation is using apitte core: https://github.com/…butte/apitte

But I've found also package Contributte/Validation which seems to implement translation of Entities (https://contributte.org/…lidator.html#…)

So, question again. Is there a way to translate validation messages in apitte or to use Contribute/Validation with Apitte?

Marek Bartoš
Nette Blogger | 1275
+
0
-

If I remember correctly, core messages are hardcoded and therefore translation is not supported.

symfony/validator (implemented by contributte/validator) requires you to define message for each constraint to be translatable https://symfony.com/…lations.html
But that is useful only if you actually use validator for request body entities
I am not sure anymore how it's hooked internally, but usually it should not matter if you register validator yourself or via an extension (contributte/validator)

Last edited by Marek Bartoš (2023-07-03 17:10)

Yess
Member | 16
+
0
-

Marek Bartoš wrote:

If I remember correctly, core messages are hardcoded and therefore translation is not supported.

symfony/validator (implemented by contributte/validator) requires you to define message for each constraint to be translatable https://symfony.com/…lations.html
But that is useful only if you actually use validator for request body entities
I am not sure anymore how it's hooked internally, but usually it should not matter if you register validator yourself or via an extension (contributte/validator)

That should work if I can somewhat use contributte/validator within cantributte/apitte, but it seems it uses its own implementation of validation.. That's why I'm confused.

Yess
Member | 16
+
0
-

So I have working solution (in case if someone need this). I've extended Apitte\Core\Mapping\Validator\SymfonyValidator and override method its validate() to inject translator into $validationBuilder.

Note: You have to also setTranslatorDomain otherwise it wont work.



    /**
     * @param object $entity
     * @throws ValidationException
     */
    public function validate($entity): void {
        $validatorBuilder = Validation::createValidatorBuilder()
            ->enableAnnotationMapping()
            ->setDoctrineAnnotationReader($this->reader)
            ->setTranslator($this->translator) // <- add this
            ->setTranslationDomain('validators'); // <- and this
      ...
   }


Next register it in neon like this


services:
    apiValidator:
        factory: App\Core\Api\ApiValidator
        setup:
            - setConstraintValidatorFactory(Contributte\Validator\ContainerConstraintValidatorFactory())

api:
    ...
    plugins:
        ...
        Apitte\Core\DI\Plugin\CoreMappingPlugin:
            request:
                validator: @apiValidator # set your validator here
        ...
    ...

Last edited by Yess (2023-07-04 11:59)