Skip to content

Commit

Permalink
Fix the rendering of HTML contents in translated help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Oct 27, 2023
1 parent 7d63640 commit 4b170b9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Resources/views/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
{% if has_input_groups %}</div>{% endif %}

{% if field.help ?? false %}
<small class="form-help">{{ field.help|raw|trans(label_translation_parameters, translation_domain) }}</small>
<small class="form-help">{{ field.help|trans(label_translation_parameters, translation_domain)|raw }}</small>
{% elseif form.vars.help ?? false %}
<small class="form-help">{{ form.vars.help|trans(form.vars.help_translation_parameters, form.vars.translation_domain)|raw }}</small>
{% endif %}
Expand Down
49 changes: 49 additions & 0 deletions tests/Controller/FormFieldHelpControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller;

use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\FormFieldHelpController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;

class FormFieldHelpControllerTest extends AbstractCrudTestCase
{
protected EntityRepository $blogPosts;

protected function setUp(): void
{
parent::setUp();
$this->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('<b>Lorem</b> Ipsum <em class="foo">2</em>', $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 <a href="https://example.com">Ipsum</a> <b>4</b>', $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.');
}
}
34 changes: 34 additions & 0 deletions tests/TestApplication/src/Controller/FormFieldHelpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;
use function Symfony\Component\Translation\t;

/**
* Used to test the ->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('<b>Lorem</b> Ipsum <em class="foo">2</em>'),
DateTimeField::new('createdAt')->setHelp(t('Lorem Ipsum 3')),
DateTimeField::new('publishedAt')->setHelp(t('Lorem <a href="https://example.com">Ipsum</a> <b>4</b>')),
];
}
}

0 comments on commit 4b170b9

Please sign in to comment.