Skip to content

Commit cf1aa90

Browse files
mpiotnicolas-grekas
authored andcommitted
[Form][TwigBridge] Add help_html option
1 parent 939ffa8 commit cf1aa90

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
88
be mandatory in 5.0.
9+
* Added `ControllerTrait::isFormValid()`
10+
* Added an `help_html` form option to display the `help` text as HTML
911

1012
4.2.0
1113
-----
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php if (!empty($help)): ?>
22
<?php $help_attr['class'] = isset($help_attr['class']) ? trim($help_attr['class'].' help-text') : 'help-text'; ?>
3-
<p id="<?php echo $view->escape($id); ?>_help" <?php echo ' '.$view['form']->block($form, 'attributes', ['attr' => $help_attr]); ?>><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($help, [], $translation_domain) : $help); ?></p>
3+
<?php $help = false !== $translation_domain ? $view['translator']->trans($help, [], $translation_domain) : $help; ?>
4+
<?php $help = false === $help_html ? $view->escape($help) : $help ?>
5+
<p id="<?php echo $view->escape($id); ?>_help" <?php echo ' '.$view['form']->block($form, 'attributes', ['attr' => $help_attr]); ?>><?php echo $help; ?></p>
46
<?php endif; ?>

Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,86 @@ public function testHelpAttr()
113113
);
114114
}
115115

116+
public function testHelpHtmlDefaultIsFalse()
117+
{
118+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
119+
'help' => 'Help <b>text</b> test!',
120+
]);
121+
$view = $form->createView();
122+
$html = $this->renderHelp($view);
123+
124+
$this->assertMatchesXpath($html,
125+
'/p
126+
[@id="name_help"]
127+
[@class="help-text"]
128+
[.="[trans]Help <b>text</b> test![/trans]"]
129+
'
130+
);
131+
132+
$this->assertMatchesXpath($html,
133+
'/p
134+
[@id="name_help"]
135+
[@class="help-text"]
136+
/b
137+
[.="text"]
138+
', 0
139+
);
140+
}
141+
142+
public function testHelpHtmlIsFalse()
143+
{
144+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
145+
'help' => 'Help <b>text</b> test!',
146+
'help_html' => false,
147+
]);
148+
$view = $form->createView();
149+
$html = $this->renderHelp($view);
150+
151+
$this->assertMatchesXpath($html,
152+
'/p
153+
[@id="name_help"]
154+
[@class="help-text"]
155+
[.="[trans]Help <b>text</b> test![/trans]"]
156+
'
157+
);
158+
159+
$this->assertMatchesXpath($html,
160+
'/p
161+
[@id="name_help"]
162+
[@class="help-text"]
163+
/b
164+
[.="text"]
165+
', 0
166+
);
167+
}
168+
169+
public function testHelpHtmlIsTrue()
170+
{
171+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
172+
'help' => 'Help <b>text</b> test!',
173+
'help_html' => true,
174+
]);
175+
$view = $form->createView();
176+
$html = $this->renderHelp($view);
177+
178+
$this->assertMatchesXpath($html,
179+
'/p
180+
[@id="name_help"]
181+
[@class="help-text"]
182+
[.="[trans]Help <b>text</b> test![/trans]"]
183+
', 0
184+
);
185+
186+
$this->assertMatchesXpath($html,
187+
'/p
188+
[@id="name_help"]
189+
[@class="help-text"]
190+
/b
191+
[.="text"]
192+
'
193+
);
194+
}
195+
116196
protected function renderForm(FormView $view, array $vars = [])
117197
{
118198
return (string) $this->engine->get('form')->form($view, $vars);

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"symfony/dom-crawler": "~3.4|~4.0",
4141
"symfony/polyfill-intl-icu": "~1.0",
4242
"symfony/security": "~3.4|~4.0",
43-
"symfony/form": "^4.2",
43+
"symfony/form": "^4.3",
4444
"symfony/expression-language": "~3.4|~4.0",
4545
"symfony/messenger": "^4.2",
4646
"symfony/mime": "^4.3",
@@ -69,7 +69,7 @@
6969
"symfony/asset": "<3.4",
7070
"symfony/console": "<3.4",
7171
"symfony/dotenv": "<4.2",
72-
"symfony/form": "<4.2",
72+
"symfony/form": "<4.3",
7373
"symfony/messenger": "<4.2",
7474
"symfony/property-info": "<3.4",
7575
"symfony/serializer": "<4.2",

0 commit comments

Comments
 (0)