From 9aeaea06fcc74181690f446ed5e2d5875a23a118 Mon Sep 17 00:00:00 2001 From: Ken Stanley Date: Tue, 9 Oct 2018 15:39:40 -0400 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=98symbol=E2=80=99=20option=20to=20?= =?UTF-8?q?PercentType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/Form/bootstrap_4_layout.html.twig | 14 ++++--- .../Form/bootstrap_base_layout.html.twig | 10 +++-- .../views/Form/form_div_layout.html.twig | 2 +- .../views/Form/foundation_5_layout.html.twig | 18 +++++--- .../AbstractBootstrap3LayoutTest.php | 37 +++++++++++++++++ .../AbstractBootstrap4LayoutTest.php | 41 +++++++++++++++++++ .../views/Form/percent_widget.html.php | 14 ++++++- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Form/Extension/Core/Type/PercentType.php | 12 ++++++ .../Form/Tests/AbstractLayoutTest.php | 28 +++++++++++++ 10 files changed, 161 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig index 0b301b42cc..03109bdf6c 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig @@ -106,12 +106,16 @@ {%- endblock dateinterval_widget %} {% block percent_widget -%} -
- {{- block('form_widget_simple') -}} -
- % + {%- if symbol -%} +
+ {{- block('form_widget_simple') -}} +
+ {{ symbol|default('%') }} +
-
+ {%- else -%} + {{- block('form_widget_simple') -}} + {%- endif -%} {%- endblock percent_widget %} {% block file_widget -%} diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig index 136cabccb1..a6ee019a09 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig @@ -26,10 +26,14 @@ {%- endblock money_widget %} {% block percent_widget -%} -
+ {%- if symbol -%} +
+ {{- block('form_widget_simple') -}} + {{ symbol|default('%') }} +
+ {%- else -%} {{- block('form_widget_simple') -}} - % -
+ {%- endif -%} {%- endblock percent_widget %} {% block datetime_widget -%} diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 52a639a333..1d7ad1c328 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -196,7 +196,7 @@ {%- block percent_widget -%} {%- set type = type|default('text') -%} - {{ block('form_widget_simple') }} % + {{ block('form_widget_simple') }}{% if symbol %} {{ symbol|default('%') }}{% endif %} {%- endblock percent_widget -%} {%- block password_widget -%} diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig index 3ae67935fb..9547ea4900 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig @@ -43,12 +43,18 @@ {% block percent_widget -%}
-
- {{- block('form_widget_simple') -}} -
-
- % -
+ {%- if symbol -%} +
+ {{- block('form_widget_simple') -}} +
+
+ {{ symbol|default('%') }} +
+ {%- else -%} +
+ {{- block('form_widget_simple') -}} +
+ {%- endif -%}
{%- endblock percent_widget %} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php index 254f9a4d1f..91fb73f646 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php @@ -2173,6 +2173,43 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest ); } + public function testPercentNoSymbol() + { + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => 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="10"] +' + ); + } + + public function testPercentCustomSymbol() + { + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => '‱')); + + $this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')), +'/div + [@class="input-group"] + [ + ./input + [@id="my&id"] + [@type="text"] + [@name="name"] + [@class="my&class form-control"] + [@value="10"] + /following-sibling::span + [@class="input-group-addon"] + [contains(.., "‱")] + ] +' + ); + } + public function testCheckedRadio() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true); diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php index d2263ea870..47c1385dbc 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php @@ -1125,6 +1125,47 @@ abstract class AbstractBootstrap4LayoutTest extends AbstractBootstrap3LayoutTest [contains(.., "%")] ] ] +' + ); + } + + public function testPercentNoSymbol() + { + $form = $this->factory->createNamed('name', PercentType::class, 0.1, array('symbol' => 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="10"] +' + ); + } + + public function testPercentCustomSymbol() + { + $form = $this->factory->createNamed('name', PercentType::class, 0.1, array('symbol' => '‱')); + + $this->assertWidgetMatchesXpath($form->createView(), array('id' => 'my&id', 'attr' => array('class' => 'my&class')), + '/div + [@class="input-group"] + [ + ./input + [@id="my&id"] + [@type="text"] + [@name="name"] + [@class="my&class form-control"] + [@value="10"] + /following-sibling::div + [@class="input-group-append"] + [ + ./span + [@class="input-group-text"] + [contains(.., "‱")] + ] + ] ' ); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php index f5839ebf70..b5552543bc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php @@ -1 +1,13 @@ -block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']) ?> % + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$symbol = $symbol !== false ? (!empty($symbol) ? ' ' . $symbol : ' %') : ''; +echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']) . $symbol; ?> diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index f1b399cedc..4644e928ef 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 4.3.0 ----- + * added a `symbol` option to the `PercentType` that allows to disable the output of the percent character * Using the `format` option of `DateType` and `DateTimeType` when the `html5` option is enabled is deprecated. * Using names for buttons that do not start with a letter, a digit, or an underscore is deprecated and will lead to an exception in 5.0. diff --git a/src/Symfony/Component/Form/Extension/Core/Type/PercentType.php b/src/Symfony/Component/Form/Extension/Core/Type/PercentType.php index dedb9e8cdd..d45a85f2cf 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/PercentType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/PercentType.php @@ -14,6 +14,8 @@ namespace Symfony\Component\Form\Extension\Core\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class PercentType extends AbstractType @@ -26,6 +28,14 @@ class PercentType extends AbstractType $builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type'])); } + /** + * {@inheritdoc} + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['symbol'] = $options['symbol']; + } + /** * {@inheritdoc} */ @@ -33,6 +43,7 @@ class PercentType extends AbstractType { $resolver->setDefaults([ 'scale' => 0, + 'symbol' => '%', 'type' => 'fractional', 'compound' => false, ]); @@ -43,6 +54,7 @@ class PercentType extends AbstractType ]); $resolver->setAllowedTypes('scale', 'int'); + $resolver->setAllowedTypes('symbol', array('bool', 'string')); } /** diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 5b4b84e9e5..d30de9b37a 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -1945,6 +1945,34 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase ); } + public function testPercentNoSymbol() + { + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => false)); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/input + [@type="text"] + [@name="name"] + [@value="10"] + [not(contains(.., "%"))] +' + ); + } + + public function testPercentCustomSymbol() + { + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, array('symbol' => '‱')); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/input + [@type="text"] + [@name="name"] + [@value="10"] + [contains(.., "‱")] +' + ); + } + public function testCheckedRadio() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);