Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:6.3fromVincentLanglet:validators
May 19, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/Symfony/Component/Validator/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,8 @@
* execution context.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @method $this disableTranslation()
*/
interface ConstraintViolationBuilderInterface
{
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp