8000 [FrameworkBundle][Validator] Add `framework.validation.disable_transl… · symfony/symfony@98ce3f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98ce3f0

Browse files
alexandre-dauboisfabpot
authored andcommitted
[FrameworkBundle][Validator] Add framework.validation.disable_translation config
1 parent e50af3d commit 98ce3f0

File tree

9 files changed

+31
-4
lines changed

9 files changed

+31
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Add new `framework.property_info.with_constructor_extractor` option to allow enabling or disabling the constructor extractor integration
1111
* Deprecate the `--show-arguments` option of the `container:debug` command, as arguments are now always shown
1212
* Add `RateLimiterFactoryInterface` as an alias of the `limiter` service
13+
* Add `framework.validation.disable_translation` option
1314

1415
7.2
1516
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+3
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
10621062
->end()
10631063
->end()
10641064
->end()
1065+
->booleanNode('disable_translation')
1066+
->defaultFalse()
1067+
->end()
10651068
->arrayNode('auto_mapping')
10661069
->info('A collection of namespaces for which auto-mapping will be enabled by default, or null to opt-in with the EnableAutoMapping constraint.')
10671070
->example([

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+4
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
17241724
$validatorBuilder->addMethodCall('setMappingCache', [new Reference('validator.mapping.cache.adapter')]);
17251725
}
17261726

1727+
if ($config['disable_translation'] ?? false) {
1728+
$validatorBuilder->addMethodCall('disableTranslation');
1729+
}
1730+
17271731
$container->setParameter('validator.auto_mapping', $config['auto_mapping']);
17281732
if (!$propertyInfoEnabled || !class_exists(PropertyInfoLoader::class)) {
17291733
$container->removeDefinition('validator.property_info_loader');

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
<xsd:attribute name="enable-attributes" type="xsd:boolean" />
299299
<xsd:attribute name="static-method" type="xsd:boolean" />
300300
<xsd:attribute name="translation-domain" type="xsd:string" />
301+
<xsd:attribute name="disable-translation" type="xsd:boolean" />
301302
<xsd:attribute name="strict-email" type="xsd:boolean" />
302303
<xsd:attribute name="email-validation-mode" type="email-validation-mode" />
303304
</xsd:complexType>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ protected static function getBundleDefaultConfig()
775775
'enable_attributes' => !class_exists(FullStack::class),
776776
'static_method' => ['loadValidatorMetadata'],
777777
'translation_domain' => 'validators',
778+
'disable_translation' => false,
778779
'mapping' => [
779780
'paths' => [],
780781
],

src/Symfony/Component/Validator/Context/ExecutionContext.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function __construct(
107107
private ValidatorInterface $validator,
108108
private mixed $root,
109109
private TranslatorInterface $translator,
110-
private ?string $translationDomain = null,
110+
private string|false|null $translationDomain = null,
111111
) {
112112
$this->violations = new ConstraintViolationList();
113113
$this->cachedObjectsRefs = new \SplObjectStorage();
@@ -134,7 +134,9 @@ public function setConstraint(Constraint $constraint): void
134134
public function addViolation(string|\Stringable $message, array $parameters = []): void
135135
{
136136
$this->violations->add(new ConstraintViolation(
137-
$this->translator->trans($message, $parameters, $this->translationDomain),
137+
false === $this->translationDomain ?
138+
strtr($message, $parameters) :
139+
$this->translator->trans($message, $parameters, $this->translationDomain),
138140
$message,
139141
$parameters,
140142
$this->root,

src/Symfony/Component/Validator/Context/ExecutionContextFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ExecutionContextFactory implements ExecutionContextFactoryInterface
2525
{
2626
public function __construct(
2727
private TranslatorInterface $translator,
28-
private ?string $translationDomain = null,
28+
private string|false|null $translationDomain = null,
2929
) {
3030
}
3131

src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public function testSetTranslationDomain()
9999
$this->assertSame($this->builder, $this->builder->setTranslationDomain('TRANS_DOMAIN'));
100100
}
101101

102+
public function testDisableTranslation()
103+
{
104+
$this->assertSame($this->builder, $this->builder->disableTranslation());
105+
}
106+
102107
public function testGetValidator()
103108
{
104109
$this->assertInstanceOf(RecursiveValidator::class, $this->builder->getValidator());

src/Symfony/Component/Validator/ValidatorBuilder.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ValidatorBuilder
5050
private ?ContainerInterface $groupProviderLocator = null;
5151
private ?CacheItemPoolInterface $mappingCache = null;
5252
private ?TranslatorInterface $translator = null;
53-
private ?string $translationDomain = null;
53+
private string|false|null $translationDomain = null;
5454

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

295+
/**
296+
* @return $this
297+
*/
298+
public function disableTranslation(): static
299+
{
300+
$this->translationDomain = false;
301+
302+
return $this;
303+
}
304+
295305
/**
296306
* @return $this
297307
*/

0 commit comments

Comments
 (0)
0