From 98ce3f0e9d04beadf885ad9362382ab06b8f12e2 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Tue, 27 Jun 2023 16:01:55 +0200 Subject: [PATCH] [FrameworkBundle][Validator] Add `framework.validation.disable_translation` config --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../DependencyInjection/Configuration.php | 3 +++ .../DependencyInjection/FrameworkExtension.php | 4 ++++ .../Resources/config/schema/symfony-1.0.xsd | 1 + .../Tests/DependencyInjection/ConfigurationTest.php | 1 + .../Component/Validator/Context/ExecutionContext.php | 6 ++++-- .../Validator/Context/ExecutionContextFactory.php | 2 +- .../Validator/Tests/ValidatorBuilderTest.php | 5 +++++ src/Symfony/Component/Validator/ValidatorBuilder.php | 12 +++++++++++- 9 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 9c9aecbd57eaf..49f8d634ddc07 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 33d84dd18d712..7f77b49414a7b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -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([ diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index bd04b0dbf487f..c1b777ad88f14 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -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'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 0f90f1cfc4cae..41c5d85577d58 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -298,6 +298,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index e8ca9873f1a70..4ede2c07eff19 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -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' => [], ], diff --git a/src/Symfony/Component/Validator/Context/ExecutionContext.php b/src/Symfony/Component/Validator/Context/ExecutionContext.php index 0378c3e6f916f..1fd373309d0ae 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContext.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContext.php @@ -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(); @@ -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, diff --git a/src/Symfony/Component/Validator/Context/ExecutionContextFactory.php b/src/Symfony/Component/Validator/Context/ExecutionContextFactory.php index a6a2844adfe14..39c2add35b762 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContextFactory.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContextFactory.php @@ -25,7 +25,7 @@ class ExecutionContextFactory implements ExecutionContextFactoryInterface { public function __construct( private TranslatorInterface $translator, - private ?string $translationDomain = null, + private string|false|null $translationDomain = null, ) { } diff --git a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php index c57a507e25579..ec464b748635f 100644 --- a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php +++ b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php @@ -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()); diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php index 83e543b8fe053..917f1c57a7557 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilder.php +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -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. @@ -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 */