Skip to content

Commit f8664e7

Browse files
committed
feature #30433 [Form] Allow to disable and customize PercentType symbol (Ken Stanley, OskarStark)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Form] Allow to disable and customize PercentType symbol | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #28796 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#11078 <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> ## `PercentType` `symbol` option As of this writing, Symfony will forcibly append a percentage sign (`%`) to all input fields that are of the PercentType form type. This PR will introduce a boolean flag called `symbol` that, when `false`, will not display the percentage sign. Each of the default layouts that define percent_widget will respect this option. You could also use a customised string as value for `symbol` option. By default, this new option will be set to `true` so that it maintains backward compatibility. The unit tests have been updated where appropriate, and a new unit test has been added (as appropriate). Commits ------- 53c5f41 [Form] Allow to disable and customize PercentType symbol 9aeaea0 Add ‘symbol’ option to PercentType
2 parents c877cf8 + 53c5f41 commit f8664e7

File tree

10 files changed

+152
-18
lines changed

10 files changed

+152
-18
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@
106106
{%- endblock dateinterval_widget %}
107107

108108
{% block percent_widget -%}
109-
<div class="input-group">
110-
{{- block('form_widget_simple') -}}
111-
<div class="input-group-append">
112-
<span class="input-group-text">%</span>
109+
{%- if symbol -%}
110+
<div class="input-group">
111+
{{- block('form_widget_simple') -}}
112+
<div class="input-group-append">
113+
<span class="input-group-text">{{ symbol|default('%') }}</span>
114+
</div>
113115
</div>
114-
</div>
116+
{%- else -%}
117+
{{- block('form_widget_simple') -}}
118+
{%- endif -%}
115119
{%- endblock percent_widget %}
116120

117121
{% block file_widget -%}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
{%- endblock money_widget %}
2727

2828
{% block percent_widget -%}
29-
<div class="input-group">
29+
{%- if symbol -%}
30+
<div class="input-group">
31+
{{- block('form_widget_simple') -}}
32+
<span class="input-group-addon">{{ symbol|default('%') }}</span>
33+
</div>
34+
{%- else -%}
3035
{{- block('form_widget_simple') -}}
31-
<span class="input-group-addon">%</span>
32-
</div>
36+
{%- endif -%}
3337
{%- endblock percent_widget %}
3438

3539
{% block datetime_widget -%}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196

197197
{%- block percent_widget -%}
198198
{%- set type = type|default('text') -%}
199-
{{ block('form_widget_simple') }} %
199+
{{ block('form_widget_simple') }}{% if symbol %} {{ symbol|default('%') }}{% endif %}
200200
{%- endblock percent_widget -%}
201201

202202
{%- block password_widget -%}

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@
4343

4444
{% block percent_widget -%}
4545
<div class="row collapse">
46-
<div class="small-9 large-10 columns">
47-
{{- block('form_widget_simple') -}}
48-
</div>
49-
<div class="small-3 large-2 columns">
50-
<span class="postfix">%</span>
51-
</div>
46+
{%- if symbol -%}
47+
<div class="small-9 large-10 columns">
48+
{{- block('form_widget_simple') -}}
49+
</div>
50+
<div class="small-3 large-2 columns">
51+
<span class="postfix">{{ symbol|default('%') }}</span>
52+
</div>
53+
{%- else -%}
54+
<div class="small-12 large-12 columns">
55+
{{- block('form_widget_simple') -}}
56+
</div>
57+
{%- endif -%}
5258
</div>
5359
{%- endblock percent_widget %}
5460

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

14+
use Symfony\Component\Form\Extension\Core\Type\PercentType;
1415
use Symfony\Component\Form\FormError;
1516
use Symfony\Component\Form\Tests\AbstractLayoutTest;
1617

@@ -2173,6 +2174,41 @@ public function testPercent()
21732174
);
21742175
}
21752176

2177+
public function testPercentNoSymbol()
2178+
{
2179+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false]);
2180+
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
2181+
'/input
2182+
[@id="my&id"]
2183+
[@type="text"]
2184+
[@name="name"]
2185+
[@class="my&class form-control"]
2186+
[@value="10"]
2187+
'
2188+
);
2189+
}
2190+
2191+
public function testPercentCustomSymbol()
2192+
{
2193+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '']);
2194+
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
2195+
'/div
2196+
[@class="input-group"]
2197+
[
2198+
./input
2199+
[@id="my&id"]
2200+
[@type="text"]
2201+
[@name="name"]
2202+
[@class="my&class form-control"]
2203+
[@value="10"]
2204+
/following-sibling::span
2205+
[@class="input-group-addon"]
2206+
[contains(.., "‱")]
2207+
]
2208+
'
2209+
);
2210+
}
2211+
21762212
public function testCheckedRadio()
21772213
{
21782214
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ public function testMoney()
10821082
]);
10831083

10841084
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
1085-
'/div
1085+
'/div
10861086
[@class="input-group"]
10871087
[
10881088
./div
@@ -1108,7 +1108,7 @@ public function testPercent()
11081108
$form = $this->factory->createNamed('name', PercentType::class, 0.1);
11091109

11101110
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
1111-
'/div
1111+
'/div
11121112
[@class="input-group"]
11131113
[
11141114
./input
@@ -1125,6 +1125,45 @@ public function testPercent()
11251125
[contains(.., "%")]
11261126
]
11271127
]
1128+
'
1129+
);
1130+
}
1131+
1132+
public function testPercentNoSymbol()
1133+
{
1134+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false]);
1135+
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
1136+
'/input
1137+
[@id="my&id"]
1138+
[@type="text"]
1139+
[@name="name"]
1140+
[@class="my&class form-control"]
1141+
[@value="10"]
1142+
'
1143+
);
1144+
}
1145+
1146+
public function testPercentCustomSymbol()
1147+
{
1148+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '']);
1149+
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
1150+
'/div
1151+
[@class="input-group"]
1152+
[
1153+
./input
1154+
[@id="my&id"]
1155+
[@type="text"]
1156+
[@name="name"]
1157+
[@class="my&class form-control"]
1158+
[@value="10"]
1159+
/following-sibling::div
1160+
[@class="input-group-append"]
1161+
[
1162+
./span
1163+
[@class="input-group-text"]
1164+
[contains(.., "‱")]
1165+
]
1166+
]
11281167
'
11291168
);
11301169
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<?php echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']) ?> %
1+
<?php $symbol = false !== $symbol ? ($symbol ? ' '.$symbol : ' %') : '' ?>
2+
<?php echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']).$symbol ?>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* added a `symbol` option to the `PercentType` that allows to disable or customize the output of the percent character
78
* Using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated.
89
* Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an
910
exception in 5.0.

src/Symfony/Component/Form/Extension/Core/Type/PercentType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
1616
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\Form\FormInterface;
18+
use Symfony\Component\Form\FormView;
1719
use Symfony\Component\OptionsResolver\OptionsResolver;
1820

1921
class PercentType extends AbstractType
@@ -26,13 +28,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2628
$builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type']));
2729
}
2830

31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function buildView(FormView $view, FormInterface $form, array $options)
35+
{
36+
$view->vars['symbol'] = $options['symbol'];
37+
}
38+
2939
/**
3040
* {@inheritdoc}
3141
*/
3242
public function configureOptions(OptionsResolver $resolver)
3343
{
3444
$resolver->setDefaults([
3545
'scale' => 0,
46+
'symbol' => '%',
3647
'type' => 'fractional',
3748
'compound' => false,
3849
]);
@@ -43,6 +54,7 @@ public function configureOptions(OptionsResolver $resolver)
4354
]);
4455

4556
$resolver->setAllowedTypes('scale', 'int');
57+
$resolver->setAllowedTypes('symbol', ['bool', 'string']);
4658
}
4759

4860
/**

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests;
1313

1414
use PHPUnit\Framework\SkippedTestError;
15+
use Symfony\Component\Form\Extension\Core\Type\PercentType;
1516
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
1617
use Symfony\Component\Form\FormError;
1718
use Symfony\Component\Form\FormView;
@@ -1945,6 +1946,36 @@ public function testPercent()
19451946
);
19461947
}
19471948

1949+
public function testPercentNoSymbol()
1950+
{
1951+
$this->requiresFeatureSet(403);
1952+
1953+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false]);
1954+
$this->assertWidgetMatchesXpath($form->createView(), [],
1955+
'/input
1956+
[@type="text"]
1957+
[@name="name"]
1958+
[@value="10"]
1959+
[not(contains(.., "%"))]
1960+
'
1961+
);
1962+
}
1963+
1964+
public function testPercentCustomSymbol()
1965+
{
1966+
$this->requiresFeatureSet(403);
1967+
1968+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '']);
1969+
$this->assertWidgetMatchesXpath($form->createView(), [],
1970+
'/input
1971+
[@type="text"]
1972+
[@name="name"]
1973+
[@value="10"]
1974+
[contains(.., "‱")]
1975+
'
1976+
);
1977+
}
1978+
19481979
public function testCheckedRadio()
19491980
{
19501981
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);

0 commit comments

Comments
 (0)