Add ‘symbol’ option to PercentType
This commit is contained in:
parent
0034e14463
commit
9aeaea06fc
@ -106,12 +106,16 @@
|
|||||||
{%- endblock dateinterval_widget %}
|
{%- endblock dateinterval_widget %}
|
||||||
|
|
||||||
{% block percent_widget -%}
|
{% block percent_widget -%}
|
||||||
|
{%- if symbol -%}
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
{{- block('form_widget_simple') -}}
|
{{- block('form_widget_simple') -}}
|
||||||
<div class="input-group-append">
|
<div class="input-group-append">
|
||||||
<span class="input-group-text">%</span>
|
<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 -%}
|
||||||
|
@ -26,10 +26,14 @@
|
|||||||
{%- endblock money_widget %}
|
{%- endblock money_widget %}
|
||||||
|
|
||||||
{% block percent_widget -%}
|
{% block percent_widget -%}
|
||||||
|
{%- if symbol -%}
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
{{- block('form_widget_simple') -}}
|
{{- block('form_widget_simple') -}}
|
||||||
<span class="input-group-addon">%</span>
|
<span class="input-group-addon">{{ symbol|default('%') }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
{%- else -%}
|
||||||
|
{{- block('form_widget_simple') -}}
|
||||||
|
{%- endif -%}
|
||||||
{%- endblock percent_widget %}
|
{%- endblock percent_widget %}
|
||||||
|
|
||||||
{% block datetime_widget -%}
|
{% block datetime_widget -%}
|
||||||
|
@ -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 -%}
|
||||||
|
@ -43,12 +43,18 @@
|
|||||||
|
|
||||||
{% block percent_widget -%}
|
{% block percent_widget -%}
|
||||||
<div class="row collapse">
|
<div class="row collapse">
|
||||||
|
{%- if symbol -%}
|
||||||
<div class="small-9 large-10 columns">
|
<div class="small-9 large-10 columns">
|
||||||
{{- block('form_widget_simple') -}}
|
{{- block('form_widget_simple') -}}
|
||||||
</div>
|
</div>
|
||||||
<div class="small-3 large-2 columns">
|
<div class="small-3 large-2 columns">
|
||||||
<span class="postfix">%</span>
|
<span class="postfix">{{ symbol|default('%') }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
{%- else -%}
|
||||||
|
<div class="small-12 large-12 columns">
|
||||||
|
{{- block('form_widget_simple') -}}
|
||||||
|
</div>
|
||||||
|
{%- endif -%}
|
||||||
</div>
|
</div>
|
||||||
{%- endblock percent_widget %}
|
{%- endblock percent_widget %}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
@ -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(.., "‱")]
|
||||||
|
]
|
||||||
|
]
|
||||||
'
|
'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -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; ?>
|
||||||
|
@ -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.
|
||||||
|
@ -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'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user