Skip to content

Commit ab9dec3

Browse files
committed
accepts floats for input="string" in NumberType
1 parent 030396a commit ab9dec3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\CallbackTransformer;
1516
use Symfony\Component\Form\Exception\LogicException;
1617
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
1718
use Symfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
@@ -37,6 +38,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3738

3839
if ('string' === $options['input']) {
3940
$builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
41+
$builder->addModelTransformer(new CallbackTransformer(
42+
function ($value) {
43+
return \is_float($value) ? (string) $value : $value;
44+
},
45+
function ($value) {
46+
return $value;
47+
}
48+
));
4049
}
4150
}
4251

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public function testDefaultFormattingWithScaleAndStringInput(): void
7777
$this->assertSame('12345,68', $form->createView()->vars['value']);
7878
}
7979

80+
public function testStringInputWithFloatData(): void
81+
{
82+
$form = $this->factory->create(static::TESTED_TYPE, 12345.6789, [
83+
'input' => 'string',
84+
'scale' => 2,
85+
]);
86+
87+
$this->assertSame('12345,68', $form->createView()->vars['value']);
88+
}
89+
8090
public function testDefaultFormattingWithRounding(): void
8191
{
8292
$form = $this->factory->create(static::TESTED_TYPE, null, ['scale' => 0, 'rounding_mode' => \NumberFormatter::ROUND_UP]);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy