Skip to content

Commit e364f02

Browse files
bug symfony#48965 [TwigBridge] Allow floats in html5 input type number field (wimhendrikx)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [TwigBridge] Allow floats in html5 input type number field | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? |no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Adding the attribute step: any allows for correct use of floats in number input Commits ------- a6f2bcd [TwigBridge] Allow floats in html5 input type number field
2 parents cfda9b4 + a6f2bcd commit e364f02

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,25 @@ public function testRenderNumberWithHtml5NumberType()
21542154
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
21552155
'/input
21562156
[@type="number"]
2157+
[@step="any"]
2158+
[@name="name"]
2159+
[@class="my&class form-control"]
2160+
[@value="1234.56"]
2161+
'
2162+
);
2163+
}
2164+
2165+
public function testRenderNumberWithHtml5NumberTypeAndStepAttribute()
2166+
{
2167+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
2168+
'html5' => true,
2169+
'attr' => ['step' => '0.1'],
2170+
]);
2171+
2172+
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
2173+
'/input
2174+
[@type="number"]
2175+
[@step="0.1"]
21572176
[@name="name"]
21582177
[@class="my&class form-control"]
21592178
[@value="1234.56"]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
4747
{
4848
if ($options['html5']) {
4949
$view->vars['type'] = 'number';
50+
51+
if (!isset($view->vars['attr']['step'])) {
52+
$view->vars['attr']['step'] = 'any';
53+
}
5054
}
5155
}
5256

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,26 @@ public function testRenderNumberWithHtml5NumberType()
18771877
$this->assertWidgetMatchesXpath($form->createView(), [],
18781878
'/input
18791879
[@type="number"]
1880+
[@step="any"]
1881+
[@name="name"]
1882+
[@value="1234.56"]
1883+
'
1884+
);
1885+
}
1886+
1887+
public function testRenderNumberWithHtml5NumberTypeAndStepAttribute()
1888+
{
1889+
$this->requiresFeatureSet(403);
1890+
1891+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\NumberType', 1234.56, [
1892+
'html5' => true,
1893+
'attr' => ['step' => '0.1'],
1894+
]);
1895+
1896+
$this->assertWidgetMatchesXpath($form->createView(), [],
1897+
'/input
1898+
[@type="number"]
1899+
[@step="0.1"]
18801900
[@name="name"]
18811901
[@value="1234.56"]
18821902
'

0 commit comments

Comments
 (0)