From aee5571a7149814543616a2b6a2e1c86556ffc3d Mon Sep 17 00:00:00 2001 From: Romaric Drigon Date: Tue, 6 Oct 2020 15:04:34 +0200 Subject: [PATCH] [Form] fix ViolationMapper was always generating a localized label for each FormType --- .../ViolationMapper/ViolationMapper.php | 72 ++++++++++--------- .../ViolationMapper/ViolationMapperTest.php | 21 ++++++ 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php index 3888e30586..8aa75be2d7 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php @@ -131,44 +131,46 @@ class ViolationMapper implements ViolationMapperInterface // Only add the error if the form is synchronized if ($this->acceptsErrors($scope)) { - $labelFormat = $scope->getConfig()->getOption('label_format'); - - if (null !== $labelFormat) { - $label = str_replace( - [ - '%name%', - '%id%', - ], - [ - $scope->getName(), - (string) $scope->getPropertyPath(), - ], - $labelFormat - ); - } else { - $label = $scope->getConfig()->getOption('label'); - } - - if (null === $label && null !== $this->formRenderer) { - $label = $this->formRenderer->humanize($scope->getName()); - } elseif (null === $label) { - $label = $scope->getName(); - } - - if (false !== $label && null !== $this->translator) { - $label = $this->translator->trans( - $label, - $scope->getConfig()->getOption('label_translation_parameters', []), - $scope->getConfig()->getOption('translation_domain') - ); - } - $message = $violation->getMessage(); $messageTemplate = $violation->getMessageTemplate(); - if (false !== $label) { - $message = str_replace('{{ label }}', $label, $message); - $messageTemplate = str_replace('{{ label }}', $label, $messageTemplate); + if (false !== strpos($message, '{{ label }}') || false !== strpos($messageTemplate, '{{ label }}')) { + $labelFormat = $scope->getConfig()->getOption('label_format'); + + if (null !== $labelFormat) { + $label = str_replace( + [ + '%name%', + '%id%', + ], + [ + $scope->getName(), + (string) $scope->getPropertyPath(), + ], + $labelFormat + ); + } else { + $label = $scope->getConfig()->getOption('label'); + } + + if (false !== $label) { + if (null === $label && null !== $this->formRenderer) { + $label = $this->formRenderer->humanize($scope->getName()); + } elseif (null === $label) { + $label = $scope->getName(); + } + + if (null !== $this->translator) { + $label = $this->translator->trans( + $label, + $scope->getConfig()->getOption('label_translation_parameters', []), + $scope->getConfig()->getOption('translation_domain') + ); + } + + $message = str_replace('{{ label }}', $label, $message); + $messageTemplate = str_replace('{{ label }}', $label, $messageTemplate); + } } $scope->addError(new FormError( diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index b25a3426ae..afd6660614 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -1738,4 +1738,25 @@ class ViolationMapperTest extends TestCase $this->assertSame('Message Translated 2nd Custom Label', $error->getMessage()); } } + + public function testTranslatorNotCalledWithoutLabel() + { + $renderer = $this->getMockBuilder(FormRenderer::class) + ->setMethods(null) + ->disableOriginalConstructor() + ->getMock() + ; + $translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); + $translator->expects($this->never())->method('trans'); + $this->mapper = new ViolationMapper($renderer, $translator); + + $parent = $this->getForm('parent'); + $child = $this->getForm('name', 'name'); + $parent->add($child); + + $parent->submit([]); + + $violation = new ConstraintViolation('Message without label', null, [], null, 'data.name', null); + $this->mapper->mapViolation($violation, $parent); + } }