Add ‘symbol’ option to PercentType

This commit is contained in:
Ken Stanley 2018-10-09 15:39:40 -04:00 committed by Oskar Stark
parent 0034e14463
commit 9aeaea06fc
10 changed files with 161 additions and 16 deletions

View File

@ -106,12 +106,16 @@
{%- endblock dateinterval_widget %} {%- endblock dateinterval_widget %}
{% block percent_widget -%} {% block percent_widget -%}
<div class="input-group"> {%- if symbol -%}
{{- block('form_widget_simple') -}} <div class="input-group">
<div class="input-group-append"> {{- block('form_widget_simple') -}}
<span class="input-group-text">%</span> <div class="input-group-append">
<span class="input-group-text">{{ symbol|default('%') }}</span>
</div>
</div> </div>
</div> {%- else -%}
{{- block('form_widget_simple') -}}
{%- endif -%}
{%- endblock percent_widget %} {%- endblock percent_widget %}
{% block file_widget -%} {% block file_widget -%}

View File

@ -26,10 +26,14 @@
{%- endblock money_widget %} {%- endblock money_widget %}
{% block percent_widget -%} {% block percent_widget -%}
<div class="input-group"> {%- if symbol -%}
<div class="input-group">
{{- block('form_widget_simple') -}}
<span class="input-group-addon">{{ symbol|default('%') }}</span>
</div>
{%- else -%}
{{- block('form_widget_simple') -}} {{- block('form_widget_simple') -}}
<span class="input-group-addon">%</span> {%- endif -%}
</div>
{%- endblock percent_widget %} {%- endblock percent_widget %}
{% block datetime_widget -%} {% block datetime_widget -%}

View File

@ -196,7 +196,7 @@
{%- block percent_widget -%} {%- block percent_widget -%}
{%- set type = type|default('text') -%} {%- set type = type|default('text') -%}
{{ block('form_widget_simple') }} % {{ block('form_widget_simple') }}{% if symbol %} {{ symbol|default('%') }}{% endif %}
{%- endblock percent_widget -%} {%- endblock percent_widget -%}
{%- block password_widget -%} {%- block password_widget -%}

View File

@ -43,12 +43,18 @@
{% block percent_widget -%} {% block percent_widget -%}
<div class="row collapse"> <div class="row collapse">
<div class="small-9 large-10 columns"> {%- if symbol -%}
{{- block('form_widget_simple') -}} <div class="small-9 large-10 columns">
</div> {{- block('form_widget_simple') -}}
<div class="small-3 large-2 columns"> </div>
<span class="postfix">%</span> <div class="small-3 large-2 columns">
</div> <span class="postfix">{{ symbol|default('%') }}</span>
</div>
{%- else -%}
<div class="small-12 large-12 columns">
{{- block('form_widget_simple') -}}
</div>
{%- endif -%}
</div> </div>
{%- endblock percent_widget %} {%- endblock percent_widget %}

View File

@ -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() public function testCheckedRadio()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true); $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);

View File

@ -1125,6 +1125,47 @@ abstract class AbstractBootstrap4LayoutTest extends AbstractBootstrap3LayoutTest
[contains(.., "%")] [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(.., "")]
]
]
' '
); );
} }

View File

@ -1 +1,13 @@
<?php echo $view['form']->block($form, 'form_widget_simple', ['type' => isset($type) ? $type : 'text']) ?> % <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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; ?>

View File

@ -4,6 +4,7 @@ CHANGELOG
4.3.0 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 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 * 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. exception in 5.0.

View File

@ -14,6 +14,8 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer; use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
class PercentType extends AbstractType class PercentType extends AbstractType
@ -26,6 +28,14 @@ class PercentType extends AbstractType
$builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type'])); $builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type']));
} }
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['symbol'] = $options['symbol'];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -33,6 +43,7 @@ class PercentType extends AbstractType
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'scale' => 0, 'scale' => 0,
'symbol' => '%',
'type' => 'fractional', 'type' => 'fractional',
'compound' => false, 'compound' => false,
]); ]);
@ -43,6 +54,7 @@ class PercentType extends AbstractType
]); ]);
$resolver->setAllowedTypes('scale', 'int'); $resolver->setAllowedTypes('scale', 'int');
$resolver->setAllowedTypes('symbol', array('bool', 'string'));
} }
/** /**

View File

@ -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() public function testCheckedRadio()
{ {
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true); $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);