Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42e8f5e

Browse files
committed
add option to render NumberType as type="number"
1 parent 346491c commit 42e8f5e

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
{%- endblock dateinterval_widget -%}
171171

172172
{%- block number_widget -%}
173-
{# type="number" doesn't work with floats #}
173+
{# type="number" doesn't work with floats in localized formats #}
174174
{%- set type = type|default('text') -%}
175175
{{ block('form_widget_simple') }}
176176
{%- endblock number_widget -%}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,22 @@ public function testNumber()
20892089
);
20902090
}
20912091

2092+
public function testRenderNumberWithHtml5NumberType()
2093+
{
2094+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
2095+
'html5' => true,
2096+
]);
2097+
2098+
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
2099+
'/input
2100+
[@type="number"]
2101+
[@name="name"]
2102+
[@class="my&class form-control"]
2103+
[@value="1234.56"]
2104+
'
2105+
);
2106+
}
2107+
20922108
public function testPassword()
20932109
{
20942110
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', 'foo&bar');

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* added `html5` option to `NumberType` that allows to render `type="number"` input fields
78
* deprecated using the `date_format`, `date_widget`, and `time_widget` options of the `DateTimeType` when the `widget`
89
option is set to `single_text`
910
* added `block_prefix` option to `BaseType`.

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
7777
protected $roundingMode;
7878

7979
private $scale;
80+
private $locale;
8081

81-
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP)
82+
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP, string $locale = null)
8283
{
8384
if (null === $grouping) {
8485
$grouping = false;
@@ -91,6 +92,7 @@ public function __construct(int $scale = null, ?bool $grouping = false, ?int $ro
9192
$this->scale = $scale;
9293
$this->grouping = $grouping;
9394
$this->roundingMode = $roundingMode;
95+
$this->locale = $locale;
9496
}
9597

9698
/**
@@ -214,7 +216,7 @@ public function reverseTransform($value)
214216
*/
215217
protected function getNumberFormatter()
216218
{
217-
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
219+
$formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::DECIMAL);
218220

219221
if (null !== $this->scale) {
220222
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);

src/Symfony/Component/Form/Extension/Core/Type/NumberType.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Exception\LogicException;
1516
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
1617
use Symfony\Component\Form\FormBuilderInterface;
18+
use Symfony\Component\Form\FormInterface;
19+
use Symfony\Component\Form\FormView;
20+
use Symfony\Component\OptionsResolver\Options;
1721
use Symfony\Component\OptionsResolver\OptionsResolver;
1822

1923
class NumberType extends AbstractType
@@ -26,10 +30,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2630
$builder->addViewTransformer(new NumberToLocalizedStringTransformer(
2731
$options['scale'],
2832
$options['grouping'],
29-
$options['rounding_mode']
33+
$options['rounding_mode'],
34+
$options['html5'] ? 'en' : null
3035
));
3136
}
3237

38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function buildView(FormView $view, FormInterface $form, array $options)
42+
{
43+
if ($options['html5']) {
44+
$view->vars['type'] = 'number';
45+
}
46+
}
47+
3348
/**
3449
* {@inheritdoc}
3550
*/
@@ -41,6 +56,7 @@ public function configureOptions(OptionsResolver $resolver)
4156
'grouping' => false,
4257
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALF_UP,
4358
'compound' => false,
59+
'html5' => false,
4460
]);
4561

4662
$resolver->setAllowedValues('rounding_mode', [
@@ -54,6 +70,15 @@ public function configureOptions(OptionsResolver $resolver)
5470
]);
5571

5672
$resolver->setAllowedTypes('scale', ['null', 'int']);
73+
$resolver->setAllowedTypes('html5', 'bool');
74+
75+
$resolver->setNormalizer('grouping', function (Options $options, $value) {
76+
if (true === $value && $options['html5']) {
77+
throw new LogicException('Cannot use the "grouping" option when the "html5" option is enabled.');
78+
}
79+
80+
return $value;
81+
});
5782
}
5883

5984
/**

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,23 @@ public function testNumber()
18711871
);
18721872
}
18731873

1874+
public function testRenderNumberWithHtml5NumberType()
1875+
{
1876+
$this->requiresFeatureSet(403);
1877+
1878+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
1879+
'html5' => true,
1880+
]);
1881+
1882+
$this->assertWidgetMatchesXpath($form->createView(), [],
1883+
'/input
1884+
[@type="number"]
1885+
[@name="name"]
1886+
[@value="1234.56"]
1887+
'
1888+
);
1889+
}
1890+
18741891
public function testPassword()
18751892
{
18761893
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', 'foo&bar');

src/Symfony/Component/Form/Tests/Extension/Core/Type/NumberTypeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,28 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedD
7575
$this->assertSame($expectedData, $form->getNormData());
7676
$this->assertSame($expectedData, $form->getData());
7777
}
78+
79+
public function testIgnoresDefaultLocaleToRenderHtml5NumberWidgets()
80+
{
81+
$form = $this->factory->create(static::TESTED_TYPE, null, [
82+
'scale' => 2,
83+
'rounding_mode' => \NumberFormatter::ROUND_UP,
84+
'html5' => true,
85+
]);
86+
$form->setData(12345.54321);
87+
88+
$this->assertSame('12345.55', $form->createView()->vars['value']);
89+
$this->assertSame('12345.55', $form->getViewData());
90+
}
91+
92+
/**
93+
* @expectedException \Symfony\Component\Form\Exception\LogicException
94+
*/
95+
public function testGroupingNotAllowedWithHtml5Widget()
96+
{
97+
$this->factory->create(static::TESTED_TYPE, null, [
98+
'grouping' => true,
99+
'html5' => true,
100+
]);
101+
}
78102
}

0 commit comments

Comments
 (0)