Skip to content

Commit 56f33ca

Browse files
committed
Merge branch '2.8' into 3.4
* 2.8: fixed deprecated messages in tests [HttpCache] Unlink tmp file on error Added LB translation for #26327 (Errors sign for people that do not see colors) [TwigBridge] Fix rendering of currency by MoneyType [HttpKernel] DumpDataCollector: do not flush when a dumper is provided
2 parents 0c80472 + f548b37 commit 56f33ca

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-1
lines changed

Extension/FormExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function getFilters()
9696
{
9797
return array(
9898
new TwigFilter('humanize', array('Symfony\Component\Form\FormRenderer', 'humanize')),
99+
new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
99100
);
100101
}
101102

@@ -166,6 +167,22 @@ public function __unset($name)
166167
unset($this->$name);
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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,83 @@
1414
{{- parent() -}}
1515
{%- endblock button_widget %}
1616

17+
{% block money_widget -%}
18+
{% set prepend = not (money_pattern starts with '{{') %}
19+
{% set append = not (money_pattern ends with '}}') %}
20+
{% if prepend or append %}
21+
<div class="input-group">
22+
{% if prepend %}
23+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
24+
{% endif %}
25+
{{- block('form_widget_simple') -}}
26+
{% if append %}
27+
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
28+
{% endif %}
29+
</div>
30+
{% else %}
31+
{{- block('form_widget_simple') -}}
32+
{% endif %}
33+
{%- endblock money_widget %}
34+
35+
{% block percent_widget -%}
36+
<div class="input-group">
37+
{{- block('form_widget_simple') -}}
38+
<span class="input-group-addon">%</span>
39+
</div>
40+
{%- endblock percent_widget %}
41+
42+
{% block datetime_widget -%}
43+
{% if widget == 'single_text' %}
44+
{{- block('form_widget_simple') -}}
45+
{% else -%}
46+
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%}
47+
<div {{ block('widget_container_attributes') }}>
48+
{{- form_errors(form.date) -}}
49+
{{- form_errors(form.time) -}}
50+
{{- form_widget(form.date, { datetime: true } ) -}}
51+
{{- form_widget(form.time, { datetime: true } ) -}}
52+
</div>
53+
{%- endif %}
54+
{%- endblock datetime_widget %}
55+
56+
{% block date_widget -%}
57+
{% if widget == 'single_text' %}
58+
{{- block('form_widget_simple') -}}
59+
{% else -%}
60+
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%}
61+
{% if datetime is not defined or not datetime -%}
62+
<div {{ block('widget_container_attributes') -}}>
63+
{%- endif %}
64+
{{- date_pattern|replace({
65+
'{{ year }}': form_widget(form.year),
66+
'{{ month }}': form_widget(form.month),
67+
'{{ day }}': form_widget(form.day),
68+
})|raw -}}
69+
{% if datetime is not defined or not datetime -%}
70+
</div>
71+
{%- endif -%}
72+
{% endif %}
73+
{%- endblock date_widget %}
74+
75+
{% block time_widget -%}
76+
{% if widget == 'single_text' %}
77+
{{- block('form_widget_simple') -}}
78+
{% else -%}
79+
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%}
80+
{% if datetime is not defined or false == datetime -%}
81+
<div {{ block('widget_container_attributes') -}}>
82+
{%- endif -%}
83+
{{- form_widget(form.hour) }}{% if with_minutes %}:{{ form_widget(form.minute) }}{% endif %}{% if with_seconds %}:{{ form_widget(form.second) }}{% endif %}
84+
{% if datetime is not defined or false == datetime -%}
85+
</div>
86+
{%- endif -%}
87+
{% endif %}
88+
{%- endblock time_widget %}
89+
90+
{% block choice_widget_collapsed -%}
91+
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
92+
{%- endblock choice_widget_collapsed %}
93+
1794
{% block checkbox_widget -%}
1895
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
1996
{% if 'checkbox-inline' in parent_label_class %}

Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
{%- endblock integer_widget -%}
178178

179179
{%- block money_widget -%}
180-
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
180+
{{ money_pattern|form_encode_currency(block('form_widget_simple')) }}
181181
{%- endblock money_widget -%}
182182

183183
{%- block url_widget -%}

Tests/Extension/FormExtensionBootstrap3LayoutTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
7575
$this->assertSame('<form name="form" method="get" action="0">', $html);
7676
}
7777

78+
public function testMoneyWidgetInIso()
79+
{
80+
$environment = new Environment(new StubFilesystemLoader(array(
81+
__DIR__.'/../../Resources/views/Form',
82+
__DIR__.'/Fixtures/templates/form',
83+
)), array('strict_variables' => true));
84+
$environment->addExtension(new TranslationExtension(new StubTranslator()));
85+
$environment->addExtension($this->extension);
86+
$environment->setCharset('ISO-8859-1');
87+
88+
$this->extension->initRuntime($environment);
89+
90+
$view = $this->factory
91+
->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\MoneyType')
92+
->createView()
93+
;
94+
95+
$this->assertSame(<<<'HTML'
96+
<div class="input-group">
97+
<span class="input-group-addon">&euro; </span>
98+
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
99+
HTML
100+
, trim($this->renderWidget($view)));
101+
}
102+
78103
protected function renderForm(FormView $view, array $vars = array())
79104
{
80105
return (string) $this->renderer->renderBlock($view, 'form', $vars);

Tests/Extension/FormExtensionDivLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ public function testIsRootForm($expected, FormView $formView)
165165
$this->assertSame($expected, \Symfony\Bridge\Twig\Extension\twig_is_root_form($formView));
166166
}
167167

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

0 commit comments

Comments
 (0)