Skip to content

Commit 6d01aa6

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [Validator] Add a section for custom translation domain
2 parents 382a97d + 27dc68d commit 6d01aa6

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
@@ -120,5 +120,64 @@ Now, create a ``validators`` catalog file in the ``translations/`` directory:
120120
'author.name.not_blank' => 'Please enter an author name.',
121121
];
122122
123-
You may need to clear your cache (even in the dev environment) after creating this
124-
file for the first time.
123+
You may need to clear your cache (even in the dev environment) after creating
124+
this file for the first time.
125+
126+
Custom Translation Domain
127+
-------------------------
128+
129+
The default translation domain can be changed globally using the
130+
``FrameworkBundle`` configuration:
131+
132+
.. configuration-block::
133+
134+
.. code-block:: yaml
135+
136+
# config/packages/validator.yaml
137+
framework:
138+
validation:
139+
translation_domain: validation_errors
140+
141+
.. code-block:: xml
142+
143+
<!-- config/packages/validator.xml -->
144+
<?xml version="1.0" encoding="UTF-8" ?>
145+
<container xmlns="http://symfony.com/schema/dic/services"
146+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
147+
xmlns:framework="http://symfony.com/schema/dic/symfony"
148+
xsi:schemaLocation="http://symfony.com/schema/dic/services
149+
https://symfony.com/schema/dic/services/services-1.0.xsd
150+
http://symfony.com/schema/dic/symfony
151+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
152+
153+
<framework:config>
154+
<framework:validation
155+
translation-domain="validation_errors"
156+
/>
157+
</framework:config>
158+
</container>
159+
160+
.. code-block:: php
161+
162+
// config/packages/validator.php
163+
use Symfony\Config\FrameworkConfig;
164+
165+
return static function (FrameworkConfig $framework) {
166+
// ...
167+
$framework
168+
->validation()
169+
->translationDomain('validation_errors')
170+
;
171+
};
172+
173+
Or it can be customized for a specific violation from a constraint validator::
174+
175+
public function validate($value, Constraint $constraint): void
176+
{
177+
// validation logic
178+
179+
$this->context->buildViolation($constraint->message)
180+
->setParameter('{{ string }}', $value)
181+
->setTranslationDomain('validation_errors')
182+
->addViolation();
183+
}

0 commit comments

Comments
 (0)