Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aef515f

Browse filesBrowse files
VincentLangletnicolas-grekas
authored andcommitted
[Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints
1 parent 825fd03 commit aef515f
Copy full SHA for aef515f

File tree

5 files changed

+33
-10
lines changed
Filter options

5 files changed

+33
-10
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Add the `filenameMaxLength` option to the `File` constraint
1414
* Add the `exclude` option to the `Cascade` constraint
1515
* Add the `value_length` parameter to `Length` constraint
16+
* Allow to disable the translation domain for `ConstraintViolationInterface` messages
1617

1718
6.2
1819
---

‎src/Symfony/Component/Validator/Context/ExecutionContext.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Context/ExecutionContext.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function setConstraint(Constraint $constraint): void
142142
public function addViolation(string $message, array $parameters = []): void
143143
{
144144
$this->violations->add(new ConstraintViolation(
145-
$this->translator->trans($message, $parameters, $this->translationDomain),
145+
false === $this->translationDomain ? strtr($message, $parameters) : $this->translator->trans($message, $parameters, $this->translationDomain),
146146
$message,
147147
$parameters,
148148
$this->root,

‎src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\ConstraintViolation;
1818
use Symfony\Component\Validator\ConstraintViolationList;
1919
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
20+
use Symfony\Contracts\Translation\TranslatorInterface;
2021

2122
class ConstraintViolationBuilderTest extends TestCase
2223
{
@@ -83,6 +84,18 @@ public function testCauseCanBeSet()
8384
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid(), $cause));
8485
}
8586

87+
public function testTranslationDomainFalse()
88+
{
89+
$translator = $this->createMock(TranslatorInterface::class);
90+
$translator->expects(self::once())->method('trans');
91+
92+
$builder = new ConstraintViolationBuilder($this->violations, new Valid(), $this->messageTemplate, [], $this->root, 'data', 'foo', $translator);
93+
$builder->addViolation();
94+
95+
$builder->disableTranslation();
96+
$builder->addViolation();
97+
}
98+
8699
private function assertViolationEquals(ConstraintViolation $expectedViolation)
87100
{
88101
$this->assertCount(1, $this->violations);

‎src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php
+16-9Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
3333
private mixed $invalidValue;
3434
private string $propertyPath;
3535
private TranslatorInterface $translator;
36-
private ?string $translationDomain;
36+
private string|false|null $translationDomain;
3737
private ?int $plural = null;
3838
private ?Constraint $constraint;
3939
private ?string $code = null;
4040
private mixed $cause = null;
4141

42-
public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null)
42+
public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string|false $translationDomain = null)
4343
{
4444
$this->violations = $violations;
4545
$this->message = $message;
@@ -80,6 +80,16 @@ public function setTranslationDomain(string $translationDomain): static
8080
return $this;
8181
}
8282

83+
/**
84+
* @return $this
85+
*/
86+
public function disableTranslation(): static
87+
{
88+
$this->translationDomain = false;
89+
90+
return $this;
91+
}
92+
8393
public function setInvalidValue(mixed $invalidValue): static
8494
{
8595
$this->invalidValue = $invalidValue;
@@ -110,16 +120,13 @@ public function setCause(mixed $cause): static
110120

111121
public function addViolation(): void
112122
{
113-
if (null === $this->plural) {
114-
$translatedMessage = $this->translator->trans(
115-
$this->message,
116-
$this->parameters,
117-
$this->translationDomain
118-
);
123+
$parameters = null === $this->plural ? $this->parameters : (['%count%' => $this->plural] + $this->parameters);
124+
if (false === $this->translationDomain) {
125+
$translatedMessage = strtr($this->message, $parameters);
119126
} else {
120127
$translatedMessage = $this->translator->trans(
121128
$this->message,
122-
['%count%' => $this->plural] + $this->parameters,
129+
$parameters,
123130
$this->translationDomain
124131
);
125132
}

‎src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* execution context.
2121
*
2222
* @author Bernhard Schussek <bschussek@gmail.com>
23+
*
24+
* @method $this disableTranslation()
2325
*/
2426
interface ConstraintViolationBuilderInterface
2527
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.