diff --git a/validation/translations.rst b/validation/translations.rst index 3f7f461aacd..721273562c1 100644 --- a/validation/translations.rst +++ b/validation/translations.rst @@ -135,5 +135,64 @@ Now, create a ``validators`` catalog file in the ``translations/`` directory: 'author.name.not_blank' => 'Please enter an author name.', ]; -You may need to clear your cache (even in the dev environment) after creating this -file for the first time. +You may need to clear your cache (even in the dev environment) after creating +this file for the first time. + +Custom Translation Domain +------------------------- + +The default translation domain can be changed globally using the +``FrameworkBundle`` configuration: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/validator.yaml + framework: + validation: + translation_domain: validation_errors + + .. code-block:: xml + + + + + + + + + + + .. code-block:: php + + // config/packages/validator.php + use Symfony\Config\FrameworkConfig; + + return static function (FrameworkConfig $framework) { + // ... + $framework + ->validation() + ->translationDomain('validation_errors') + ; + }; + +Or it can be customized for a specific violation from a constraint validator:: + + public function validate($value, Constraint $constraint): void + { + // validation logic + + $this->context->buildViolation($constraint->message) + ->setParameter('{{ string }}', $value) + ->setTranslationDomain('validation_errors') + ->addViolation(); + }