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

[Validator] Allow to use translation_domain false for validators and to use custom translation domain by constraints #48852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
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
[Validator] Allow to use translation_domain false for validators and …
…to use custom translation domain by constraints
  • Loading branch information
VincentLanglet authored and nicolas-grekas committed May 19, 2023
commit aef515f3b875c4ca65c827d0f3b88ebd77245458
1 change: 1 addition & 0 deletions 1 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;
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
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
stof marked this conversation as resolved.
Show resolved Hide resolved
{
$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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.