Skip to content

Commit e6b8bcb

Browse files
author
Belhassen
committed
Fix MoneyType: add missing step attribute when html5=true
1 parent baf4a16 commit e6b8bcb

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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)