[TwigBridge] Fix rendering of currency by MoneyType

This commit is contained in:
Roland Franssen 2018-03-24 17:52:09 +01:00 committed by Fabien Potencier
parent 8bebc5325d
commit a3a2ff0c74
10 changed files with 96 additions and 6 deletions

View File

@ -88,6 +88,7 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface
{
return array(
new TwigFilter('humanize', array($this, 'humanize')),
new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
);
}
@ -166,6 +167,22 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface
return null === $formView->parent;
}
/**
* @internal
*/
public function encodeCurrency(Environment $environment, $text, $widget = '')
{
if ('UTF-8' === $charset = $environment->getCharset()) {
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
} else {
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
$text = iconv('UTF-8', $charset, $text);
$widget = iconv('UTF-8', $charset, $widget);
}
return str_replace('{{ widget }}', $widget, $text);
}
/**
* {@inheritdoc}
*/

View File

@ -25,11 +25,11 @@
{% if prepend or append %}
<div class="input-group">
{% if prepend %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
{% endif %}
{{- block('form_widget_simple') -}}
{% if append %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
{% endif %}
</div>
{% else %}

View File

@ -142,7 +142,7 @@
{%- endblock integer_widget -%}
{%- block money_widget -%}
{{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
{{ money_pattern|form_encode_currency(block('form_widget_simple')) }}
{%- endblock money_widget -%}
{%- block url_widget -%}

View File

@ -63,6 +63,31 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
$this->extension = null;
}
public function testMoneyWidgetInIso()
{
$environment = new Environment(new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
)), array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension($this->extension);
$environment->setCharset('ISO-8859-1');
$this->extension->initRuntime($environment);
$view = $this->factory
->createNamed('name', 'money')
->createView()
;
$this->assertSame(<<<'HTML'
<div class="input-group">
<span class="input-group-addon">&euro; </span>
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
HTML
, trim($this->renderWidget($view)));
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

View File

@ -162,6 +162,26 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
$this->assertSame($expected, $this->extension->isRootForm($formView));
}
public function testMoneyWidgetInIso()
{
$environment = new Environment(new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
)), array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension($this->extension);
$environment->setCharset('ISO-8859-1');
$this->extension->initRuntime($environment);
$view = $this->factory
->createNamed('name', 'money')
->createView()
;
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

View File

@ -1 +1 @@
<?php echo str_replace('{{ widget }}', $view['form']->block($form, 'form_widget_simple'), $money_pattern) ?>
<?php echo $view['form']->formEncodeCurrency($money_pattern, $view['form']->block($form, 'form_widget_simple')) ?>

View File

@ -260,4 +260,20 @@ class FormHelper extends Helper
{
return $this->renderer->humanize($text);
}
/**
* @internal
*/
public function formEncodeCurrency($text, $widget = '')
{
if ('UTF-8' === $charset = $this->getCharset()) {
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
} else {
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
$text = iconv('UTF-8', $charset, $text);
$widget = iconv('UTF-8', $charset, $widget);
}
return str_replace('{{ widget }}', $widget, $text);
}
}

View File

@ -61,6 +61,18 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
parent::tearDown();
}
public function testMoneyWidgetInIso()
{
$this->engine->setCharset('ISO-8859-1');
$view = $this->factory
->createNamed('name', 'money')
->createView()
;
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->form($view, $vars);

View File

@ -30,7 +30,7 @@
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
"symfony/security-csrf": "~2.6",
"symfony/stopwatch": "~2.3",
"symfony/templating": "~2.1",
"symfony/templating": "~2.7",
"symfony/translation": "~2.7",
"doctrine/annotations": "~1.0"
},

View File

@ -83,7 +83,7 @@ class MoneyType extends AbstractType
}
/**
* Returns the pattern for this locale.
* Returns the pattern for this locale in UTF-8.
*
* The pattern contains the placeholder "{{ widget }}" where the HTML tag should
* be inserted