Skip to content

Commit

Permalink
[Form] Allow to disable and customise PercentType symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Mar 4, 2019
1 parent 7951ea1 commit 8f3160c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 17 deletions.
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
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
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
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\Extension\Core\Type\PercentType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Tests\AbstractLayoutTest;

Expand Down Expand Up @@ -2173,6 +2174,40 @@ public function testPercent()
);
}

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::span
[@class="input-group-addon"]
[contains(.., "‱")]
]
'
);
}

public function testCheckedRadio()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\RadioType', true);
Expand Down
Expand Up @@ -1082,7 +1082,7 @@ public function testMoney()
]);

$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
'/div
'/div
[@class="input-group"]
[
./div
Expand All @@ -1108,7 +1108,7 @@ public function testPercent()
$form = $this->factory->createNamed('name', PercentType::class, 0.1);

$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
'/div
'/div
[@class="input-group"]
[
./input
Expand All @@ -1125,6 +1125,44 @@ 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
@@ -1 +1,14 @@
<?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', array('type' => isset($type) ? $type : 'text')) . $symbol; ?>

1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -41,6 +41,7 @@ CHANGELOG
```
* added the `input_format` option to `DateType`, `DateTimeType`, and `TimeType` to specify the input format when setting
the `input` option to `string`
* added a `symbol` option to the `PercentType` that allows to disable or customise the output of the percent character

4.2.0
-----
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/PercentType.php
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([
'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
26 changes: 26 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests;

use PHPUnit\Framework\SkippedTestError;
use Symfony\Component\Form\Extension\Core\Type\PercentType;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormView;
Expand Down Expand Up @@ -1945,6 +1946,31 @@ public function testPercent()
);
}

public function testPercentNoSymbol()
{
$form = $this->factory->createNamed('name', PercentType::class, 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', PercentType::class, 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

0 comments on commit 8f3160c

Please sign in to comment.