diff --git a/src/Resources/views/crud/form_theme.html.twig b/src/Resources/views/crud/form_theme.html.twig index c5ce8b0fb6..00170de46c 100644 --- a/src/Resources/views/crud/form_theme.html.twig +++ b/src/Resources/views/crud/form_theme.html.twig @@ -100,7 +100,7 @@ {% if has_input_groups %}{% endif %} {% if field.help ?? false %} - {{ field.help|raw|trans(label_translation_parameters, translation_domain) }} + {{ field.help|trans(label_translation_parameters, translation_domain)|raw }} {% elseif form.vars.help ?? false %} {{ form.vars.help|trans(form.vars.help_translation_parameters, form.vars.translation_domain)|raw }} {% endif %} diff --git a/tests/Controller/FormFieldHelpControllerTest.php b/tests/Controller/FormFieldHelpControllerTest.php new file mode 100644 index 0000000000..a158c1a678 --- /dev/null +++ b/tests/Controller/FormFieldHelpControllerTest.php @@ -0,0 +1,49 @@ +client->followRedirects(); + + $this->blogPosts = $this->entityManager->getRepository(BlogPost::class); + } + + protected function getControllerFqcn(): string + { + return FormFieldHelpController::class; + } + + protected function getDashboardFqcn(): string + { + return DashboardController::class; + } + + public function testFieldsWithoutHelp() + { + $crawler = $this->client->request('GET', $this->generateNewFormUrl()); + + static::assertSelectorNotExists('.form-group #BlogPost_id + .form-help', 'The ID field does not define a help message.'); + + static::assertSelectorNotExists('.form-group #BlogPost_title + .form-help', 'The title field defines an empty string as a help message, so it does not render an HTML element for that help message.'); + + static::assertSelectorTextContains('.form-group #BlogPost_slug + .form-help', 'Lorem Ipsum 1', 'The slug field defines a text help message.'); + + static::assertSame('Lorem Ipsum 2', $crawler->filter('.form-group #BlogPost_content + .form-help')->html(), 'The content field defines an help message with HTML contents, which must be rendered instead of escaped.'); + + static::assertSelectorTextContains('.form-group:contains("Created At") .form-help', 'Lorem Ipsum 3', 'The createdAt field defines a translatable text help message.'); + + static::assertSame('Lorem Ipsum 4', $crawler->filter('.form-group:contains("Published At") .form-help')->html(), 'The publishedAt field defines a translatable help message with HTML contents, which must be rendered instead of escaped.'); + } +} diff --git a/tests/TestApplication/src/Controller/FormFieldHelpController.php b/tests/TestApplication/src/Controller/FormFieldHelpController.php new file mode 100644 index 0000000000..a04cc7ad34 --- /dev/null +++ b/tests/TestApplication/src/Controller/FormFieldHelpController.php @@ -0,0 +1,34 @@ +setHelp() method of fields and how that + * help message is rendered in the form. + */ +class FormFieldHelpController extends AbstractCrudController +{ + public static function getEntityFqcn(): string + { + return BlogPost::class; + } + + public function configureFields(string $pageName): iterable + { + return [ + IdField::new('id'), // this field doesn't define a help message on purpose + TextField::new('title')->setHelp(''), + TextField::new('slug')->setHelp('Lorem Ipsum 1'), + TextField::new('content')->setHelp('Lorem Ipsum 2'), + DateTimeField::new('createdAt')->setHelp(t('Lorem Ipsum 3')), + DateTimeField::new('publishedAt')->setHelp(t('Lorem Ipsum 4')), + ]; + } +}