Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aeaea0

Browse files
Ken StanleyOskarStark
Ken Stanley
authored andcommitted
Add ‘symbol’ option to PercentType
1 parent 0034e14 commit 9aeaea0

File tree

10 files changed

+161
-16
lines changed

10 files changed

+161
-16
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,43 @@ public function testPercent()
21732173
);
21742174
}
21752175

2176+
public function testPercentNoSymbol()
2177+
{
2178+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => false));
2179+
2180+
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('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', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => ''));
2194+
2195+
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')),
2196+
'/div
2197+
[@class="input-group"]
2198+
[
2199+
./input
2200+
[@id="my&id"]
2201+
[@type="text"]
2202+
[@name="name"]
2203+
[@class="my&class form-control"]
2204+
[@value="10"]
2205+
/following-sibling::span
2206+
[@class="input-group-addon"]
2207+
[contains(.., "‱")]
2208+
]
2209+
'
2210+
);
2211+
}
2212+
21762213
public function testCheckedRadio()
21772214
{
21782215
$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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,47 @@ 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, array('symbol' => false));
1135+
1136+
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')),
1137+
'/input
1138+
[@id="my&id"]
1139+
[@type="text"]
1140+
[@name="name"]
1141+
[@class="my&class form-control"]
1142+
[@value="10"]
1143+
'
1144+
);
1145+
}
1146+
1147+
public function testPercentCustomSymbol()
1148+
{
1149+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, array('symbol' => ''));
1150+
1151+
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')),
1152+
'/div
1153+
[@class="input-group"]
1154+
[
1155+
./input
1156+
[@id="my&id"]
1157+
[@type="text"]
1158+
[@name="name"]
1159+
[@class="my&class form-control"]
1160+
[@value="10"]
1161+
/following-sibling::div
1162+
[@class="input-group-append"]
1163+
[
1164+
./span
1165+
[@class="input-group-text"]
1166+
[contains(.., "‱")]
1167+
]
1168+
]
11281169
'
11291170
);
11301171
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
<?php echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']) ?> %
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$symbol = $symbol !== false ? (!empty($symbol) ? ' ' . $symbol : ' %') : '';
13+
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 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', array('bool', 'string'));
4658
}
4759

4860
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,34 @@ public function testPercent()
19451945
);
19461946
}
19471947

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

0 commit comments

Comments
 (0)