Skip to content

Commit

Permalink
bug #25233 [TwigBridge][Form] Fix hidden currency element with Bootst…
Browse files Browse the repository at this point in the history
…rap 3 theme (julienfalque)

This PR was merged into the 2.7 branch.

Discussion
----------

[TwigBridge][Form] Fix hidden currency element with Bootstrap 3 theme

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

When using a `MoneyType` field, one can pass `currency=false` option to hide the currency symbol. This does not work well when using the Bootstrap 3 theme: the symbol is not displayed but HTML elements that are supposed to contain it are still rendered.

Commits
-------

c5af7fd Fix hidden currency element with Bootstrap 3 theme
  • Loading branch information
fabpot committed Dec 11, 2017
2 parents 891b321 + c5af7fd commit 9ac08e8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Expand Up @@ -20,16 +20,21 @@
{%- endblock %}

{% block money_widget -%}
<div class="input-group">
{% set append = money_pattern starts with '{{' %}
{% if not append %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
{% endif %}
{% set prepend = not (money_pattern starts with '{{') %}
{% set append = not (money_pattern ends with '}}') %}
{% if prepend or append %}
<div class="input-group">
{% if prepend %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
{% endif %}
{{- block('form_widget_simple') -}}
{% if append %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
{% endif %}
</div>
{% else %}
{{- block('form_widget_simple') -}}
{% if append %}
<span class="input-group-addon">{{ money_pattern|replace({ '{{ widget }}':''}) }}</span>
{% endif %}
</div>
{% endif %}
{%- endblock money_widget %}

{% block percent_widget -%}
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
Expand Up @@ -1899,6 +1899,25 @@ public function testMoney()
);
}

public function testMoneyWithoutCurrency()
{
$form = $this->factory->createNamed('name', 'money', 1234.56, array(
'currency' => false,
));

$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')),
'/input
[@id="my&id"]
[@type="text"]
[@name="name"]
[@class="my&class form-control"]
[@value="1234.56"]
[not(preceding-sibling::*)]
[not(following-sibling::*)]
'
);
}

public function testNumber()
{
$form = $this->factory->createNamed('name', 'number', 1234.56);
Expand Down
Expand Up @@ -43,7 +43,7 @@ public function testMoneyPatternWorksForYen()
$view = $this->factory->create(static::TESTED_TYPE, null, array('currency' => 'JPY'))
->createView();

$this->assertTrue((bool) strstr($view->vars['money_pattern'], '¥'));
$this->assertSame('¥ {{ widget }}', $view->vars['money_pattern']);
}

// https://github.com/symfony/symfony/issues/5458
Expand All @@ -62,4 +62,12 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull($expected, $norm, '');
}

public function testMoneyPatternWithoutCurrency()
{
$view = $this->factory->create(static::TESTED_TYPE, null, array('currency' => false))
->createView();

$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
}
}

0 comments on commit 9ac08e8

Please sign in to comment.