Skip to content

Commit a55e834

Browse files
committed
[Form] Allow TranslatableInterface to the FormType help option
1 parent b8c4aae commit a55e834

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Allow passing `TranslatableInterface` objects to the `ChoiceView` label
8+
* Allow passing `TranslatableInterface` objects to the `help` option
89

910
6.1
1011
---

Extension/Core/Type/FormType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Symfony\Component\OptionsResolver\OptionsResolver;
2525
use Symfony\Component\PropertyAccess\PropertyAccess;
2626
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27-
use Symfony\Component\Translation\TranslatableMessage;
27+
use Symfony\Contracts\Translation\TranslatableInterface;
2828

2929
class FormType extends BaseType
3030
{
@@ -220,7 +220,7 @@ public function configureOptions(OptionsResolver $resolver)
220220
$resolver->setAllowedTypes('label_attr', 'array');
221221
$resolver->setAllowedTypes('action', 'string');
222222
$resolver->setAllowedTypes('upload_max_size_message', ['callable']);
223-
$resolver->setAllowedTypes('help', ['string', 'null', TranslatableMessage::class]);
223+
$resolver->setAllowedTypes('help', ['string', 'null', TranslatableInterface::class]);
224224
$resolver->setAllowedTypes('help_attr', 'array');
225225
$resolver->setAllowedTypes('help_html', 'bool');
226226
$resolver->setAllowedTypes('is_empty_callback', ['null', 'callable']);

Tests/AbstractLayoutTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Symfony\Component\Form\Test\FormIntegrationTestCase;
2121
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
2222
use Symfony\Component\Translation\TranslatableMessage;
23+
use Symfony\Contracts\Translation\TranslatableInterface;
24+
use Symfony\Contracts\Translation\TranslatorInterface;
2325

2426
abstract class AbstractLayoutTest extends FormIntegrationTestCase
2527
{
@@ -2699,6 +2701,28 @@ public function testHelpWithTranslatableMessage()
26992701
);
27002702
}
27012703

2704+
public function testHelpWithTranslatableInterface()
2705+
{
2706+
$message = new class() implements TranslatableInterface {
2707+
public function trans(TranslatorInterface $translator, string $locale = null): string
2708+
{
2709+
return $translator->trans('foo');
2710+
}
2711+
};
2712+
2713+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
2714+
'help' => $message,
2715+
]);
2716+
$html = $this->renderHelp($form->createView());
2717+
2718+
$this->assertMatchesXpath($html,
2719+
'/*
2720+
[@id="name_help"]
2721+
[.="[trans]foo[/trans]"]
2722+
'
2723+
);
2724+
}
2725+
27022726
public function testAttributesWithTranslationParameters()
27032727
{
27042728
$this->requiresFeatureSet(403);

0 commit comments

Comments
 (0)