Skip to content

Commit 642eba5

Browse files
HeahDudejaviereguiluz
authored andcommitted
[Validator] Add a section for custom translation domain
1 parent 2cbbb39 commit 642eba5

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

validation/translations.rst

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,64 @@ Now, create a ``validators`` catalog file in the ``translations/`` directory:
135135
'author.name.not_blank' => 'Please enter an author name.',
136136
];
137137
138-
You may need to clear your cache (even in the dev environment) after creating this
139-
file for the first time.
138+
You may need to clear your cache (even in the dev environment) after creating
139+
this file for the first time.
140+
141+
Custom Translation Domain
142+
-------------------------
143+
144+
The default translation domain can be changed globally using the
145+
``FrameworkBundle`` configuration:
146+
147+
.. configuration-block::
148+
149+
.. code-block:: yaml
150+
151+
# config/packages/validator.yaml
152+
framework:
153+
validation:
154+
translation_domain: validation_errors
155+
156+
.. code-block:: xml
157+
158+
<!-- config/packages/validator.xml -->
159+
<?xml version="1.0" encoding="UTF-8" ?>
160+
<container xmlns="http://symfony.com/schema/dic/services"
161+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
162+
xmlns:framework="http://symfony.com/schema/dic/symfony"
163+
xsi:schemaLocation="http://symfony.com/schema/dic/services
164+
https://symfony.com/schema/dic/services/services-1.0.xsd
165+
http://symfony.com/schema/dic/symfony
166+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
167+
168+
<framework:config>
169+
<framework:validation
170+
translation-domain="validation_errors"
171+
/>
172+
</framework:config>
173+
</container>
174+
175+
.. code-block:: php
176+
177+
// config/packages/validator.php
178+
use Symfony\Config\FrameworkConfig;
179+
180+
return static function (FrameworkConfig $framework) {
181+
// ...
182+
$framework
183+
->validation()
184+
->translationDomain('validation_errors')
185+
;
186+
};
187+
188+
Or it can be customized for a specific violation from a constraint validator::
189+
190+
public function validate($value, Constraint $constraint): void
191+
{
192+
// validation logic
193+
194+
$this->context->buildViolation($constraint->message)
195+
->setParameter('{{ string }}', $value)
196+
->setTranslationDomain('validation_errors')
197+
->addViolation();
198+
}

0 commit comments

Comments
 (0)