Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b5d6bf8

Browse filesBrowse files
committed
Allow string input
1 parent 8b00851 commit b5d6bf8
Copy full SHA for b5d6bf8

File tree

Expand file treeCollapse file tree

4 files changed

+59
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+59
-2
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for displaying nested options in DebugCommand
8+
* Add support for strings as data for the `MoneyType`
89

910
7.2
1011
---

‎src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function reverseTransform(mixed $value): int|float|null
7070
if (null !== $value) {
7171
$value = (string) ($value * $this->divisor);
7272

73-
if ('float' === $this->input) {
73+
if ('integer' !== $this->input) {
7474
return (float) $value;
7575
}
7676

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\Exception\LogicException;
1616
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
17+
use Symfony\Component\Form\Extension\Core\DataTransformer\StringToFloatTransformer;
1718
use Symfony\Component\Form\FormBuilderInterface;
1819
use Symfony\Component\Form\FormInterface;
1920
use Symfony\Component\Form\FormView;
@@ -38,6 +39,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3839
$options['input'],
3940
))
4041
;
42+
43+
if ('string' === $options['input']) {
44+
$builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
45+
}
4146
}
4247

4348
public function buildView(FormView $view, FormInterface $form, array $options): void
@@ -77,7 +82,7 @@ public function configureOptions(OptionsResolver $resolver): void
7782

7883
$resolver->setAllowedTypes('html5', 'bool');
7984

80-
$resolver->setAllowedValues('input', ['float', 'integer']);
85+
$resolver->setAllowedValues('input', ['float', 'integer', 'string']);
8186

8287
$resolver->setNormalizer('grouping', static function (Options $options, $value) {
8388
if ($value && $options['html5']) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

14+
use Symfony\Component\Form\Exception\TransformationFailedException;
1415
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

@@ -146,4 +147,54 @@ public function testIntegerInputWithoutDivisor()
146147

147148
$this->assertSame(1234567, $form->getData());
148149
}
150+
151+
public function testDefaultFormattingWithScaleAndStringInput()
152+
{
153+
$form = $this->factory->create(static::TESTED_TYPE, null, ['scale' => 2, 'input' => 'string']);
154+
$form->setData('12345.67890');
155+
156+
$this->assertSame('12345.68', $form->createView()->vars['value']);
157+
}
158+
159+
public function testStringInputWithFloatData()
160+
{
161+
$this->expectException(TransformationFailedException::class);
162+
$this->expectExceptionMessage('Expected a numeric string.');
163+
164+
$this->factory->create(static::TESTED_TYPE, 12345.6789, [
165+
'input' => 'string',
166+
'scale' => 2,
167+
]);
168+
}
169+
170+
public function testStringInputWithIntData()
171+
{
172+
$this->expectException(TransformationFailedException::class);
173+
$this->expectExceptionMessage('Expected a numeric string.');
174+
175+
$this->factory->create(static::TESTED_TYPE, 12345, [
176+
'input' => 'string',
177+
'scale' => 2,
178+
]);
179+
}
180+
181+
public function testSubmitStringInputWithDefaultScale()
182+
{
183+
$form = $this->factory->create(static::TESTED_TYPE, null, ['input' => 'string']);
184+
$form->submit('1.234');
185+
186+
$this->assertSame('1.23', $form->getData());
187+
$this->assertSame(1.23, $form->getNormData());
188+
$this->assertSame('1.23', $form->getViewData());
189+
}
190+
191+
public function testSubmitStringInputWithScale()
192+
{
193+
$form = $this->factory->create(static::TESTED_TYPE, null, ['input' => 'string', 'scale' => 3]);
194+
$form->submit('1.234');
195+
196+
$this->assertSame('1.234', $form->getData());
197+
$this->assertSame(1.234, $form->getNormData());
198+
$this->assertSame('1.234', $form->getViewData());
199+
}
149200
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.