diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 262bf3a08e05f..fc974c5a185a9 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -13,6 +13,7 @@ CHANGELOG * Add the `filenameMaxLength` option to the `File` constraint * Add the `exclude` option to the `Cascade` constraint * Add the `value_length` parameter to `Length` constraint + * Allow to disable the translation domain for `ConstraintViolationInterface` messages 6.2 --- diff --git a/src/Symfony/Component/Validator/Context/ExecutionContext.php b/src/Symfony/Component/Validator/Context/ExecutionContext.php index bca57fa94c96d..41292ef7dd02b 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContext.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContext.php @@ -142,7 +142,7 @@ public function setConstraint(Constraint $constraint): void public function addViolation(string $message, array $parameters = []): void { $this->violations->add(new ConstraintViolation( - $this->translator->trans($message, $parameters, $this->translationDomain), + false === $this->translationDomain ? strtr($message, $parameters) : $this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, diff --git a/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php b/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php index 2dbb185629cee..cf959a334853e 100644 --- a/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php +++ b/src/Symfony/Component/Validator/Tests/Violation/ConstraintViolationBuilderTest.php @@ -17,6 +17,7 @@ use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; +use Symfony\Contracts\Translation\TranslatorInterface; class ConstraintViolationBuilderTest extends TestCase { @@ -83,6 +84,18 @@ public function testCauseCanBeSet() $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid(), $cause)); } + public function testTranslationDomainFalse() + { + $translator = $this->createMock(TranslatorInterface::class); + $translator->expects(self::once())->method('trans'); + + $builder = new ConstraintViolationBuilder($this->violations, new Valid(), $this->messageTemplate, [], $this->root, 'data', 'foo', $translator); + $builder->addViolation(); + + $builder->disableTranslation(); + $builder->addViolation(); + } + private function assertViolationEquals(ConstraintViolation $expectedViolation) { $this->assertCount(1, $this->violations); diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index c67589dc1402d..476cfacd88d0e 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -33,13 +33,13 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface private mixed $invalidValue; private string $propertyPath; private TranslatorInterface $translator; - private ?string $translationDomain; + private string|false|null $translationDomain; private ?int $plural = null; private ?Constraint $constraint; private ?string $code = null; private mixed $cause = null; - public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null) + public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string|false $translationDomain = null) { $this->violations = $violations; $this->message = $message; @@ -80,6 +80,16 @@ public function setTranslationDomain(string $translationDomain): static return $this; } + /** + * @return $this + */ + public function disableTranslation(): static + { + $this->translationDomain = false; + + return $this; + } + public function setInvalidValue(mixed $invalidValue): static { $this->invalidValue = $invalidValue; @@ -110,16 +120,13 @@ public function setCause(mixed $cause): static public function addViolation(): void { - if (null === $this->plural) { - $translatedMessage = $this->translator->trans( - $this->message, - $this->parameters, - $this->translationDomain - ); + $parameters = null === $this->plural ? $this->parameters : (['%count%' => $this->plural] + $this->parameters); + if (false === $this->translationDomain) { + $translatedMessage = strtr($this->message, $parameters); } else { $translatedMessage = $this->translator->trans( $this->message, - ['%count%' => $this->plural] + $this->parameters, + $parameters, $this->translationDomain ); } diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php index 2c1fa7e306f30..02fbeb797c547 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php @@ -20,6 +20,8 @@ * execution context. * * @author Bernhard Schussek + * + * @method $this disableTranslation() */ interface ConstraintViolationBuilderInterface { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy