From 3de453c368d1925dc1c1c4ef64fb0fe510407190 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 7 Feb 2021 15:51:08 +0100 Subject: [PATCH] merge translation parameters with value configured for parent form --- .../ViolationMapper/ViolationMapper.php | 4 +- .../ViolationMapper/ViolationMapperTest.php | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php index 48284d8d73..1982e85d8b 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php @@ -185,14 +185,16 @@ class ViolationMapper implements ViolationMapperInterface if (null !== $this->translator) { $form = $scope; + $translationParameters = $form->getConfig()->getOption('label_translation_parameters', []); do { $translationDomain = $form->getConfig()->getOption('translation_domain'); + $translationParameters = array_merge($form->getConfig()->getOption('label_translation_parameters', []), $translationParameters); } while (null === $translationDomain && null !== $form = $form->getParent()); $label = $this->translator->trans( $label, - $scope->getConfig()->getOption('label_translation_parameters', []), + $translationParameters, $translationDomain ); } 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 32975f74a5..858be09c78 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -1786,6 +1786,44 @@ class ViolationMapperTest extends TestCase $this->assertSame('Message "translated foo label"', $errors[0]->getMessage()); } + public function testLabelPlaceholderTranslatedWithTranslationParametersMergedFromParentForm() + { + $translator = $this->createMock(TranslatorInterface::class); + $translator->expects($this->any()) + ->method('trans') + ->with('foo', [ + '{{ param_defined_in_parent }}' => 'param defined in parent value', + '{{ param_defined_in_child }}' => 'param defined in child value', + '{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child child value', + ]) + ->willReturn('translated foo label') + ; + $this->mapper = new ViolationMapper(null, $translator); + + $form = $this->getForm('', null, null, [], false, true, [ + 'label_translation_parameters' => [ + '{{ param_defined_in_parent }}' => 'param defined in parent value', + '{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child parent value', + ], + ]); + $child = $this->getForm('foo', 'foo', null, [], false, true, [ + 'label' => 'foo', + 'label_translation_parameters' => [ + '{{ param_defined_in_child }}' => 'param defined in child value', + '{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child child value', + ], + ]); + $form->add($child); + + $violation = new ConstraintViolation('Message "{{ label }}"', null, [], null, 'data.foo', null); + $this->mapper->mapViolation($violation, $form); + + $errors = iterator_to_array($child->getErrors()); + + $this->assertCount(1, $errors, $child->getName().' should have an error, but has none'); + $this->assertSame('Message "translated foo label"', $errors[0]->getMessage()); + } + public function testTranslatorNotCalledWithoutLabel() { $renderer = $this->getMockBuilder(FormRenderer::class)