Skip to content

Commit fa95f51

Browse files
authored
shortened doc
1 parent f64cc0a commit fa95f51

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

validation/translations.rst

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,36 +126,28 @@ this file for the first time.
126126
Translatable Objects
127127
--------------------
128128

129-
In some cases, you may want to translate content from a simple function with multiple checks or contexts.
130-
For example, when using the ``Callback`` constraints.
131-
132-
You can use the :class:`Symfony\\Component\\Translation\\TranslatableMessage` class like this:
133-
134-
.. configuration-block::
135-
136-
.. code-block:: php-attributes
137-
138-
use Symfony\Component\Translation\TranslatableMessage;
139-
use Symfony\Component\Validator\Constraints as Assert;
140-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
141-
142-
#[Assert\Callback]
143-
public function validate(ExecutionContextInterface $context, mixed $payload): void
144-
{
145-
// somehow you have an array of "fake names"
146-
$fakeNames = [/* ... */];
147-
148-
// check if the name is actually a fake name
149-
if (in_array($this->getFirstName(), $fakeNames)) {
150-
$context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators'))
151-
->atPath('firstName')
152-
->addViolation()
153-
;
154-
}
129+
You can also use :class:`Ns\\TranslatableMessage` to build your violation message::
130+
131+
use Symfony\Component\Translation\TranslatableMessage;
132+
use Symfony\Component\Validator\Constraints as Assert;
133+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
134+
135+
#[Assert\Callback]
136+
public function validate(ExecutionContextInterface $context, mixed $payload): void
137+
{
138+
// somehow you have an array of "fake names"
139+
$fakeNames = [/* ... */];
140+
141+
// check if the name is actually a fake name
142+
if (in_array($this->getFirstName(), $fakeNames)) {
143+
$context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators'))
144+
->atPath('firstName')
145+
->addViolation()
146+
;
155147
}
148+
}
156149

157-
Learn more about
158-
:ref:`translatable object <translatable-objects>`.
150+
You can learn more about translatable messages in :ref:`the dedicated section <translatable-objects>`.
159151

160152
Custom Translation Domain
161153
-------------------------

0 commit comments

Comments
 (0)