8000 [FrameworkBundle][Validator] Add `framework.validation.disable_translation` option by alexandre-daubois · Pull Request #50797 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][Validator] Add framework.validation.disable_translation option #50797

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
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/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add new `framework.property_info.with_constructor_extractor` option to allow enabling or disabling the constructor extractor integration
* Deprecate the `--show-arguments` option of the `container:debug` command, as arguments are now always shown
* Add `RateLimiterFactoryInterface` as an alias of the `limiter` service
* Add `framework.validation.disable_translation` option

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,9 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
->end()
->end()
->end()
->booleanNode('disable_translation')
->defaultFalse()
->end()
->arrayNode('auto_mapping')
->info('A collection of namespaces for which auto-mapping will be enabled by default, or null to opt-in with the EnableAutoMapping constraint.')
->example([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
$validatorBuilder->addMethodCall('setMappingCache', [new Reference('validator.mapping.cache.adapter')]);
}

if ($config['disable_translation'] ?? false) {
$validatorBuilder->addMethodCall('disableTranslation');
}

$container->setParameter('validator.auto_mapping', $config['auto_mapping']);
if (!$propertyInfoEnabled || !class_exists(PropertyInfoLoader::class)) {
$container->removeDefinition('validator.property_info_loader');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
<xsd:attribute name="enable-attributes" type="xsd:boolean" />
<xsd:attribute name="static-method" type="xsd:boolean" />
<xsd:attribute name="translation-domain" type="xsd:string" />
<xsd:attribute name="disable-translation" type="xsd:boolean" />
<xsd:attribute name="strict-email" type="xsd:boolean" />
<xsd:attribute name="email-validation-mode" type="email-validation-mode" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ protected static function getBundleDefaultConfig()
'enable_attributes' => !class_exists(FullStack::class),
'static_method' => ['loadValidatorMetadata'],
'translation_domain' => 'validators',
'disable_translation' => false,
'mapping' => [
'paths' => [],
],
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Validator/Context/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function __construct(
private ValidatorInterface $validator,
private mixed $root,
private TranslatorInterface $translator,
private ?string $translationDomain = null,
private string|false|null $translationDomain = null,
) {
$this->violations = new ConstraintViolationList();
$this->cachedObjectsRefs = new \SplObjectStorage();
Expand All @@ -134,7 +134,9 @@ public function setConstraint(Constraint $constraint): void
public function addViolation(string|\Stringable $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 @@ -25,7 +25,7 @@ class ExecutionContextFactory implements ExecutionContextFactoryInterface
{
public function __construct(
private TranslatorInterface $translator,
private ?string $translationDomain = null,
private string|false|null $translationDomain = null,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public function testSetTranslationDomain()
$this->assertSame($this->builder, $this->builder->setTranslationDomain('TRANS_DOMAIN'));
}

public function testDisableTranslation()
{
$this->assertSame($this->builder, $this->builder->disableTranslation());
}

public function testGetValidator()
{
$this->assertInstanceOf(RecursiveValidator::class, $this->builder->getValidator());
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Validator/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ValidatorBuilder
private ?ContainerInterface $groupProviderLocator = null;
private ?CacheItemPoolInterface $mappingCache = null;
private ?TranslatorInterface $translator = null;
private ?string $translationDomain = null;
private string|false|null $translationDomain = null;

/**
* Adds an object initializer to the validator.
Expand Down Expand Up @@ -292,6 +292,16 @@ public function setTranslationDomain(?string $translationDomain): static
return $this;
}

/**
* @return $this
*/
public function disableTranslation(): static
{
$this->translationDomain = false;

return $this;
}

/**
* @return $this
*/
Expand Down
Loading
0