Skip to content

Commit 42bcbd5

Browse files
ro0NLfabpot
authored andcommitted
[TwigBridge] Fix rendering of currency by MoneyType
1 parent 925d4d7 commit 42bcbd5

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

Extension/FormExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function getFilters()
8888
{
8989
return array(
9090
new TwigFilter('humanize', array($this, 'humanize')),
91+
new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
9192
);
9293
}
9394

@@ -166,6 +167,22 @@ public function isRootForm(FormView $formView)
166167
return null === $formView->parent;
167168
}
168169

170+
/**
171+
* @internal
172+
*/
173+
public function encodeCurrency(Environment $environment, $text, $widget = '')
174+
{
175+
if ('UTF-8' === $charset = $environment->getCharset()) {
176+
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
177+
} else {
178+
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
179+
$text = iconv('UTF-8', $charset, $text);
180+
$widget = iconv('UTF-8', $charset, $widget);
181+
}
182+
183+
return str_replace('{{ widget }}', $widget, $text);
184+
}
185+
169186
/**
170187
* {@inheritdoc}
171188
*/

Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
{% if prepend or append %}
2626
<div class="input-group">
2727
{% if prepend %}
28-
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
28+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
2929
{% endif %}
3030
{{- block('form_widget_simple') -}}
3131
{% if append %}
32-
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
32+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
3333
{% endif %}
3434
</div>
3535
{% else %}

Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
{%- endblock integer_widget -%}
143143

144144
{%- block money_widget -%}
145-
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
145+
{{ money_pattern|form_encode_currency(block('form_widget_simple')) }}
146146
{%- endblock money_widget -%}
147147

148148
{%- block url_widget -%}

Tests/Extension/FormExtensionBootstrap3LayoutTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ protected function tearDown()
6363
$this->extension = null;
6464
}
6565

66+
public function testMoneyWidgetInIso()
67+
{
68+
$environment = new Environment(new StubFilesystemLoader(array(
69+
__DIR__.'/../../Resources/views/Form',
70+
__DIR__.'/Fixtures/templates/form',
71+
)), array('strict_variables' => true));
72+
$environment->addExtension(new TranslationExtension(new StubTranslator()));
73+
$environment->addExtension($this->extension);
74+
$environment->setCharset('ISO-8859-1');
75+
76+
$this->extension->initRuntime($environment);
77+
78+
$view = $this->factory
79+
->createNamed('name', 'money')
80+
->createView()
81+
;
82+
83+
$this->assertSame(<<<'HTML'
84+
<div class="input-group">
85+
<span class="input-group-addon">&euro; </span>
86+
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
87+
HTML
88+
, trim($this->renderWidget($view)));
89+
}
90+
6691
protected function renderForm(FormView $view, array $vars = array())
6792
{
6893
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

Tests/Extension/FormExtensionDivLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ public function testIsRootForm($expected, FormView $formView)
162162
$this->assertSame($expected, $this->extension->isRootForm($formView));
163163
}
164164

165+
public function testMoneyWidgetInIso()
166+
{
167+
$environment = new Environment(new StubFilesystemLoader(array(
168+
__DIR__.'/../../Resources/views/Form',
169+
__DIR__.'/Fixtures/templates/form',
170+
)), array('strict_variables' => true));
171+
$environment->addExtension(new TranslationExtension(new StubTranslator()));
172+
$environment->addExtension($this->extension);
173+
$environment->setCharset('ISO-8859-1');
174+
175+
$this->extension->initRuntime($environment);
176+
177+
$view = $this->factory
178+
->createNamed('name', 'money')
179+
->createView()
180+
;
181+
182+
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
183+
}
184+
165185
protected function renderForm(FormView $view, array $vars = array())
166186
{
167187
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

0 commit comments

Comments
 (0)