8000 feature #48852 [Validator] Allow to use translation_domain false for … · weaverryan/symfony@92e213e · GitHub
[go: up one dir, main page]

Skip to content

Commit 92e213e

Browse files
feature symfony#48852 [Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints (VincentLanglet)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #... | License | MIT | Doc PR | symfony/symfony-docs#... Currently it's possible to write ``` $this->context->addViolation($constraint->message); $this->context->addViolation($constraint->message, ['customParameter' => $value]); ``` but it's not allowed to pass a custom translation_domain (or to disable the translation by passing `false`). Adding a third parameter would allow this. (And changing the `?string` type to `string|false|null`). Wdyt about this feature ? How can we make it fully BC ? Commits ------- aef515f [Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints
2 parents 95aa09e + aef515f commit 92e213e

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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