Skip to content

Commit 69876aa

Browse files
bug #62621 [Form] Fix moneytype step (Belhassen)
This PR was merged into the 6.4 branch. Discussion ---------- [Form] Fix moneytype step | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- if yes, also update src/**/CHANGELOG.md --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Issues | Fix #62620 <!-- prefix each issue number with "Fix #"; no need to create an issue if none exists, explain below --> | License | MIT This PR fixes an inconsistency between `MoneyType` and `NumberType` when the `html5` option is enabled. `NumberType` automatically adds a `step="any"` attribute when none is provided, which allows decimal input in browsers. `MoneyType` did not add this attribute, causing inconsistent behavior and preventing decimal input in some cases. ### What this PR does - Adds `step="any"` when `html5` is `true` and no custom step is set - Ensures `MoneyType` behaves like `NumberType` ### Tests A new test has been added to ensure the behavior is correctly applied and will not regress. ### Why it matters This improves consistency across form types and fixes a long-standing UX issue mentioned in #62620. Commits ------- 768d170 Fix MoneyType: add missing step attribute when html5=true
2 parents f39e9b9 + 768d170 commit 69876aa

File tree

7 files changed

+52
-6
lines changed

7 files changed

+52
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testMoneyWidgetInIso()
7171
$this->assertSame(<<<'HTML'
7272
<div class="input-group">
7373
<span class="input-group-addon">&euro; </span>
74-
<input type="text" id="name" name="name" required="required" class="form-control" /> </div>
74+
<input type="text" id="name" name="name" required="required" inputmode="decimal" class="form-control" /> </div>
7575
HTML
7676
, trim($this->renderWidget($view)));
7777
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testMoneyWidgetInIso()
7676
$this->assertSame(<<<'HTML'
7777
<div class="input-group "><div class="input-group-prepend">
7878
<span class="input-group-text">&euro; </span>
79-
</div><input type="text" id="name" name="name" required="required" class="form-control" /></div>
79+
</div><input type="text" id="name" name="name" required="required" inputmode="decimal" class="form-control" /></div>
8080
HTML
8181
, trim($this->renderWidget($view)));
8282
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testMoneyWidgetInIso()
7575
->createView();
7676

7777
self::assertSame(<<<'HTML'
78-
<div class="input-group "><span class="input-group-text">&euro; </span><input type="text" id="name" name="name" required="required" class="form-control" /></div>
78+
<div class="input-group "><span class="input-group-text">&euro; </span><input type="text" id="name" name="name" required="required" inputmode="decimal" class="form-control" /></div>
7979
HTML
8080
, trim($this->renderWidget($view)));
8181
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testMoneyWidgetInIso()
156156
->createView()
157157
;
158158

159-
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
159+
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" inputmode="decimal" />', $this->renderWidget($view));
160160
}
161161

162162
public function testHelpAttr()

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"symfony/asset-mapper": "^6.3|^7.0",
3030
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
3131
"symfony/finder": "^5.4|^6.0|^7.0",
32-
"symfony/form": "^6.4.20|^7.2.5",
32+
"symfony/form": "^6.4.30|^7.3.8|^7.4.1",
3333
"symfony/html-sanitizer": "^6.1|^7.0",
3434
"symfony/http-foundation": "^5.4|^6.0|^7.0",
3535
"symfony/http-kernel": "^6.4|^7.0",
@@ -58,7 +58,7 @@
5858
"phpdocumentor/reflection-docblock": "<3.2.2",
5959
"phpdocumentor/type-resolver": "<1.4.0",
6060
"symfony/console": "<5.4",
61-
"symfony/form": "<6.3",
61+
"symfony/form": "<6.4",
6262
"symfony/http-foundation": "<5.4",
6363
"symfony/http-kernel": "<6.4",
6464
"symfony/mime": "<6.2",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5151

5252
if ($options['html5']) {
5353
$view->vars['type'] = 'number';
54+
55+
if (!isset($view->vars['attr']['step'])) {
56+
$view->vars['attr']['step'] = 'any';
57+
}
58+
} else {
59+
$view->vars['attr']['inputmode'] = 0 === $options['scale'] ? 'numeric' : 'decimal';
5460
}
5561
}
5662

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,44 @@ public function testHtml5EnablesSpecificFormatting()
123123
$this->assertSame('12345.60', $form->createView()->vars['value']);
124124
$this->assertSame('number', $form->createView()->vars['type']);
125125
}
126+
127+
public function testHtml5AddsStepAttributeIfNotSet()
128+
{
129+
$form = $this->factory->create(static::TESTED_TYPE, null, [
130+
'html5' => true,
131+
]);
132+
$view = $form->createView();
133+
$this->assertSame('any', $view->vars['attr']['step']);
134+
135+
$form = $this->factory->create(static::TESTED_TYPE, null, [
136+
'html5' => false,
137+
'scale' => 2,
138+
]);
139+
$view = $form->createView();
140+
$this->assertSame('decimal', $view->vars['attr']['inputmode']);
141+
142+
$form = $this->factory->create(static::TESTED_TYPE, null, [
143+
'html5' => false,
144+
'scale' => 0,
145+
]);
146+
$view = $form->createView();
147+
$this->assertSame('numeric', $view->vars['attr']['inputmode']);
148+
}
149+
150+
public function testHtml5DoesNotOverrideUserProvidedStep()
151+
{
152+
$form = $this->factory->create(static::TESTED_TYPE, null, [
153+
'html5' => true,
154+
'attr' => ['step' => '0.01'],
155+
]);
156+
$view = $form->createView();
157+
$this->assertSame('0.01', $view->vars['attr']['step']);
158+
159+
$form = $this->factory->create(static::TESTED_TYPE, null, [
160+
'html5' => false,
161+
'scale' => 2,
162+
]);
163+
$view = $form->createView();
164+
$this->assertSame('decimal', $view->vars['attr']['inputmode']);
165+
}
126166
}

0 commit comments

Comments
 (0)