Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* execution context.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @method $this disableTranslation()
*/
interface ConstraintViolationBuilderInterface
{
Expand Down