[TwigBridge] Fix rendering of currency by MoneyType
This commit is contained in:
parent
8bebc5325d
commit
a3a2ff0c74
@ -88,6 +88,7 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
new TwigFilter('humanize', array($this, 'humanize')),
|
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;
|
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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -25,11 +25,11 @@
|
|||||||
{% if prepend or append %}
|
{% if prepend or append %}
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
{% if prepend %}
|
{% if prepend %}
|
||||||
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
|
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{{- block('form_widget_simple') -}}
|
{{- block('form_widget_simple') -}}
|
||||||
{% if append %}
|
{% if append %}
|
||||||
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
|
<span class="input-group-addon">{{ money_pattern|form_encode_currency }}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -142,7 +142,7 @@
|
|||||||
{%- endblock integer_widget -%}
|
{%- endblock integer_widget -%}
|
||||||
|
|
||||||
{%- block money_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 -%}
|
{%- endblock money_widget -%}
|
||||||
|
|
||||||
{%- block url_widget -%}
|
{%- block url_widget -%}
|
||||||
|
@ -63,6 +63,31 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
|
|||||||
$this->extension = null;
|
$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">€ </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())
|
protected function renderForm(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
|
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
|
||||||
|
@ -162,6 +162,26 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
|
|||||||
$this->assertSame($expected, $this->extension->isRootForm($formView));
|
$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('€ <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
|
||||||
|
}
|
||||||
|
|
||||||
protected function renderForm(FormView $view, array $vars = array())
|
protected function renderForm(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
|
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
|
||||||
|
@ -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')) ?>
|
||||||
|
@ -260,4 +260,20 @@ class FormHelper extends Helper
|
|||||||
{
|
{
|
||||||
return $this->renderer->humanize($text);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,18 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
|||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMoneyWidgetInIso()
|
||||||
|
{
|
||||||
|
$this->engine->setCharset('ISO-8859-1');
|
||||||
|
|
||||||
|
$view = $this->factory
|
||||||
|
->createNamed('name', 'money')
|
||||||
|
->createView()
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->assertSame('€ <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
|
||||||
|
}
|
||||||
|
|
||||||
protected function renderForm(FormView $view, array $vars = array())
|
protected function renderForm(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->engine->get('form')->form($view, $vars);
|
return (string) $this->engine->get('form')->form($view, $vars);
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
|
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
|
||||||
"symfony/security-csrf": "~2.6",
|
"symfony/security-csrf": "~2.6",
|
||||||
"symfony/stopwatch": "~2.3",
|
"symfony/stopwatch": "~2.3",
|
||||||
"symfony/templating": "~2.1",
|
"symfony/templating": "~2.7",
|
||||||
"symfony/translation": "~2.7",
|
"symfony/translation": "~2.7",
|
||||||
"doctrine/annotations": "~1.0"
|
"doctrine/annotations": "~1.0"
|
||||||
},
|
},
|
||||||
|
@ -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
|
* The pattern contains the placeholder "{{ widget }}" where the HTML tag should
|
||||||
* be inserted
|
* be inserted
|
||||||
|
Reference in New Issue
Block a user