Skip to content

Feature: [Form] Toggle Displaying Percent Type Symbol #28797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@
{%- endblock dateinterval_widget %}

{% block percent_widget -%}
<div class="input-group">
{{- block('form_widget_simple') -}}
<div class="input-group-append">
<span class="input-group-text">%</span>
{%- if symbol -%}
<div class="input-group">
{{- block('form_widget_simple') -}}
<div class="input-group-append">
<span class="input-group-text">{{ symbol|default('%') }}</span>
</div>
</div>
</div>
{%- else -%}
{{- block('form_widget_simple') -}}
{%- endif -%}
{%- endblock percent_widget %}

{% block file_widget -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
{%- endblock money_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') -}}
<span class="input-group-addon">%</span>
</div>
{%- endif -%}
{%- endblock percent_widget %}

{% block datetime_widget -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,43 @@ public function testPercent()
);
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,47 @@ public function testPercent()
[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(.., "‱")]
]
]
'
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
<?php echo $view['form']->block($form, 'form_widget_simple', array('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', array('type' => isset($type) ? $type : 'text')) . $symbol; ?>
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CHANGELOG
* added a cause when a CSRF error has occurred
* deprecated the `scale` option of the `IntegerType`
* removed restriction on allowed HTTP methods
* added a `symbol` option to the `PercentType` that allows to disable the output of the percent character

4.1.0
-----
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/PercentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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
Expand All @@ -26,13 +28,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type']));
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['symbol'] = $options['symbol'];
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'scale' => 0,
'symbol' => '%',
'type' => 'fractional',
'compound' => false,
));
Expand All @@ -43,6 +54,7 @@ public function configureOptions(OptionsResolver $resolver)
));

$resolver->setAllowedTypes('scale', 'int');
$resolver->setAllowedTypes('symbol', array('bool', 'string'));
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,34 @@ public function testPercent()
);
}

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);
Expand Down