From 3b186821c8158adef48360074309b3baaf4c74cf Mon Sep 17 00:00:00 2001 From: Florian CAVASIN Date: Thu, 19 Oct 2023 18:17:21 +0200 Subject: [PATCH] [Validator] Add "Translatable Objects" example --- validation/translations.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/validation/translations.rst b/validation/translations.rst index 5826ae906ca..5b37fb30aca 100644 --- a/validation/translations.rst +++ b/validation/translations.rst @@ -123,6 +123,29 @@ Now, create a ``validators`` catalog file in the ``translations/`` directory: You may need to clear your cache (even in the dev environment) after creating this file for the first time. +You can also use :class:`Symfony\\Component\\Translation\\TranslatableMessage` to build your violation message:: + + use Symfony\Component\Translation\TranslatableMessage; + use Symfony\Component\Validator\Constraints as Assert; + use Symfony\Component\Validator\Context\ExecutionContextInterface; + + #[Assert\Callback] + public function validate(ExecutionContextInterface $context, mixed $payload): void + { + // somehow you have an array of "fake names" + $fakeNames = [/* ... */]; + + // check if the name is actually a fake name + if (in_array($this->getFirstName(), $fakeNames, true)) { + $context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators')) + ->atPath('firstName') + ->addViolation() + ; + } + } + +You can learn more about translatable messages in :ref:`the dedicated section `. + Custom Translation Domain -------------------------