Skip to content

Commit

Permalink
Merge branch '6.3' into 6.4
Browse files Browse the repository at this point in the history
* 6.3:
  [Validator] Add "Translatable Objects" example
  • Loading branch information
javiereguiluz committed Oct 25, 2023
2 parents 5a026e0 + 56c3af1 commit 9ff4de7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions validation/translations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <translatable-objects>`.

Custom Translation Domain
-------------------------

Expand Down

0 comments on commit 9ff4de7

Please sign in to comment.